PDA

View Full Version : Echo just one item from database?



skyler
April 19th, 2006, 04:43 PM
I have this code. It echo's everything from my database. i only want one item to showup from my database. And I want to be able to say which one i want to echo (using 'id')

here is my code.

//the code to connect to database goes here
$i=1;
while($row = mysql_fetch_assoc($gettable)){
echo $row['title'];
$i++;
}



assume there are no errors and this works. Tell me how I would echo only one 'id' of my choice. hurry, this would be really helpful.

SlowRoasted
April 19th, 2006, 04:49 PM
You can specify it in your sql statement.



$i = 1;
$query = "SELECT * FROM tablename WHERE id='$i'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo $row['title'];
}

skOOb
April 19th, 2006, 04:54 PM
or

$i = 1;
$result = mysql_fetch_array(mysql_query("SELECT * FROM tablename WHERE id=$i Limit 1"));
echo $result['title'];
thats if you know there will only be one result. both will work.

hth :beer:

SlowRoasted
April 19th, 2006, 04:58 PM
Yeah, only use limit one if id is not the primary key, otherwise it's not necessary. Usually id is the primary key though.:P

skyler
April 19th, 2006, 05:17 PM
HOORAY!!! Both seemed to work, but the first suggestion works better in my instance. Thank you both so much!