Jump to content

records not displaying


mindapolis

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/248498-records-not-displaying/
Share on other sites

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 />";
}
}

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 . . .
}

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 . . .
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.