Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Hudson Valley, NY
    Posts
    147
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Select only first row from query

    I'm sure this is stupid....
    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:
    PHP Code:
     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?
    Andy
    (Management+Coworkers)<>Logic

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    you can use mysql_result to get any row you want

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •