0

I am seeing the echo of an entire row of a query:

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   echo "<li>{$row[0]}</li>";
}
echo "</ul>";  

When the query is processed there is only one column (client_id) displayed from the database instead of the entire row. However, when I use

print_r(mysql_fetch_assoc($result));

just before while loop I can see displayed:

Array (
    [client_id] => 1
    [contact_first_name] => carla
    [contact_last_name] => clausen
    [business_name] => Banana Belt Liquors
    [business_address] => 300 U.S. 24 Woodlan
    [business_address2] =>
    [business_city] => Woodland Park
    [business_state] => Colorado
    [business_zip] => 80863
    [business_areaCode_phone] => 719
    [business_phone] => 687
    [business_phone2] => 9757
    [business_url] => http://bananabeltliquors.com/
    [business_email] => [email protected]
) 
3
  • 1
    Your line echo "<li>{$row[0]}</li>" is only echoing the first element of each row. That's why you don't see any more. Commented Jul 8, 2013 at 4:00
  • Look at your vardump... do you see any numeric keys in that array? Neither do I... Try echo "<li>{$row['contact_first_name']}</li>"; instead. And don't mix fetch_row and fetch_assoc. pick one style and go with it. Or use fetch_array, and get both. Commented Jul 8, 2013 at 4:01
  • This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. bg2.php.net/manual/en/function.mysql-fetch-row.php Commented Jul 8, 2013 at 4:12

3 Answers 3

1

mysql_fetch_row is fetching the entire row but as per your code you are only fetchin the first column from the entire rows.

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   print_r($row);////// this will print the entire row
   echo "<li>{$row[0]}</li>";


    echo "<li>{$row[1]}</li>";///for other columns
    echo "<li>{$row[2]}</li>";
    ---------------------
    ----------------------
    echo "<li>{$row[8]}</li>";
}
echo "</ul>"; 
Sign up to request clarification or add additional context in comments.

Comments

0

you are only echoing out the first field for each of the records. If you wanted to see them all you would change your code to:

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   echo "<li>".implode("<br/>", $row)."</li>";
}
echo "</ul>";  

This will show all of fields separated by a break for each row in a list item.

If you wanted each field to be in it's own list element you could do

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   echo "<li>".implode("</li><li>", $row)."</li>";
}
echo "</ul>"; 

Comments

0

$row will return entire row but $row[0] will have only first column. So You can count the number of columns and then iterate $row.

 echo "<ul>";
    while($row = mysql_fetch_row($result))
    {
       $len = count($row);
       for($i =0;$i<$len;$i++){
             echo "<li>{$row[$i]}</li>";
       }
    }
    echo "</ul>";  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.