really newbie, hopefully an easy answer?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

really newbie, hopefully an easy answer?

Post by illmapu »

Hi,

I am very new to php so please bear with me if this is simple, but I am stumped. :oops: Right now, I am using this code that counts the number of titles I have, but I want to disply the text from the title. Can someone help?

<?php
require('config.php');
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname,$db) or die(mysql_error());
$title=mysql_query("select title from links");
$title2=mysql_num_rows($title);
?>

This tells me how many titles I have in total, but I need to beable to show what those title descriptions are, if that makes sense?


Please help and thank you in advance!
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

That's pretty simple, you need to use http://php.net/mysql-fetch-assoc or something similar. An example peice of code would be

Code: Select all

<?php
require('config.php');
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname,$db) or die(mysql_error());
$title=mysql_query("select title from links");
$title2=mysql_num_rows($title);
while ($row = mysql_fetch_assoc($title))
{
    echo 'The title is: ' . $row['title'] . '<br>',"\n";
}
?>
That should display all of the titles you selected from the database. Hope that helps.
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

Thanks!

Post by illmapu »

:) Thank you, that worked great and the link helped great too!

Is there a trick so that the data pulled doesn't come up as alphabetically? For example, i have 2 pieces of data with numbers as the title and 2 with words, it is organizing them into numbers , then words and alphabetically. I would just like to have the titles come up from the first to the last only. Can you help or give me another great link that I could learn from?

Thanks again in advance! :P
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

Well... when you select data from a database, it just selects it in the order that it is in, in the database. If you have another field in there you want to order by then use the MySQL function ORDER BY which can be found at http://www.mysql.com/doc/en/SELECT.html What you probably want to use is ORDER BY title ASC. So your query would look something like

Code: Select all

SELECT title FROM links ORDER BY title ASC
Hope that helps!
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

Thanks again!

Post by illmapu »

:lol: Thanks again for your help, I'm gonig to start reading this forum and php.net for more answers! :wink:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I recommend taking a peek at http://se.php.net/tips.php. You can configure your browser to be setup as a quick-help, that I personally think is pretty useful.
Post Reply