Echo just one item from database?

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.

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'];
}

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:

Yeah, only use limit one if id is not the primary key, otherwise it’s not necessary. Usually id is the primary key though.:stuck_out_tongue:

HOORAY!!! Both seemed to work, but the first suggestion works better in my instance. Thank you both so much!