mindapolis Posted October 5, 2011 Share Posted October 5, 2011 Hi, can someone help me understand why it 's only printing the first record in the database ? <?php require_once("functions.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php DatabaseConnection(); mysql_select_db("auntievics"); $query= "SELECT product_id FROM treats"; $result_set= mysql_query($query); if ($result_set){ $products= mysql_fetch_row($result_set); foreach ($products as $value){ print $value; } print "<br />"; } //print_r(mysql_fetch_row($result_set)); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248498-records-not-displaying/ Share on other sites More sharing options...
Pikachu2000 Posted October 5, 2011 Share Posted October 5, 2011 You need to use a while loop (or mysql_data_seek(), but we won't go there) to access each record that's returned. The foreach() loop you're using only accesses the array associated with the one current record. if( $result_set = mysql_query($query) ) { while( $products = mysql_fetch_row($result_set) ) { foreach( $products as $value ){ print $value; } print "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/248498-records-not-displaying/#findComment-1276126 Share on other sites More sharing options...
mindapolis Posted October 5, 2011 Author Share Posted October 5, 2011 that helped so much, thank you ! I do have one more question. One of the fields in the database contains hyperlinks to pictures. How can I have it display the image, not the url ? Quote Link to comment https://forums.phpfreaks.com/topic/248498-records-not-displaying/#findComment-1276144 Share on other sites More sharing options...
Pikachu2000 Posted October 5, 2011 Share Posted October 5, 2011 Echo it as an <img> tag, with the value from the database field as the src= attribute. It's going to require you change the way everything else from each record is echoed as well. Instead of using a foreach() loop to echo each value, specify each value to be echoed separately. while( $products = mysql_fetch_row($result_set) ) { echo $products[0]; echo $products[1]; echo "<img src=\"{$products[2]}\">"; // assuming this is where the image url is etcetera . . . } Quote Link to comment https://forums.phpfreaks.com/topic/248498-records-not-displaying/#findComment-1276212 Share on other sites More sharing options...
mindapolis Posted October 6, 2011 Author Share Posted October 6, 2011 Ok, let me see if I'm following you. The $products[x] would be what field each piece of information is coming from. For example, the image field would be $products[5], right ? Here 's what I have but it 's still putting the hyprelink. $query= "SELECT * FROM treats"; $result_set= mysql_query($query); if( $result_set = mysql_query($query) ) { while( $products = mysql_fetch_row($result_set) ) { echo $products[0]; echo $products[1]; echo $products[2]; echo $products[3]; echo $products[4]; echo "<img src=\"{$products[5]}\">"; // assuming this is where the image url is } } //print_r(mysql Echo it as an <img> tag, with the value from the database field as the src= attribute. It's going to require you change the way everything else from each record is echoed as well. Instead of using a foreach() loop to echo each value, specify each value to be echoed separately. while( $products = mysql_fetch_row($result_set) ) { echo $products[0]; echo $products[1]; echo "<img src=\"{$products[2]}\">"; // assuming this is where the image url is etcetera . . . } Quote Link to comment https://forums.phpfreaks.com/topic/248498-records-not-displaying/#findComment-1276325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.