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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.