View Full Version : Select only first row from query

07-02-2007, 07:35 PM
I'm sure this is stupid....:p
I have a SELECT query that returns many rows for one item. I want to display the item once, and then loop through all the rows to build my option dropdown. I know how to do all of this EXCEPT for how to get only one row to display the item in the first place! How do you get one row out of the result:

else if ($item != "") {
while ($row = mysql_fetch_array ($itemresult))
if (($row['ITEM_NO'] == $item)){
echo $row[0];
$title = $row['ITEM_NO'] . " " . $row['DESCR'];
$item_block .= "<td><center><img src=\"" . $item_dir . "/" . mb_strtolower($row['IMG']) . "\" alt=\"" . $row['ITEM_NO'] . "\" /><br>" . "$" . $row['PRC_1'] . "<br>" . $row['ITEM_NO'] . "</center><br>";
$item_block .= $row['HTML'] . "</td>\n";
without the loop?

07-02-2007, 08:18 PM
you can use mysql_result (http://www.php.net/mysql_result) to get any row you want

07-03-2007, 08:50 AM
Not sure if this will help you, but you could always add a 'LIMIT 1' to your SQL line, and then just mysql_fetch_assoc() that query result.

$item = mysql_fetch_assoc(mysql_query("SELECT * FROM whatever LIMIT 1"));

But I wouldn't use the above unless you're positive in having a row returned from the database, otherwise do some eror checking before doing the mysql_fetch_assoc().