Jump to content

Problem with Fetch Array


cjbeck71081

Recommended Posts

I posted a question earlier regarding a fetch array, and i think i solved it myself.  Part of the way, so now my question is new.  I set up a fetch array to bring down entries into mySQL database and populate an HTML table with them.  However, the fetch array is only brining down the first entry 4 times.  There are 5 entries in the database, each of them with different information. Any help is appreciated.

http://www.epicri.com/bdd/form2.php

here is the code:

[code]<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
-->
</style>
</head>

<?php

$hostname = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
$usertable = "bddtable";
$dbName = "epicr001";
$propname = $_POST['propname'];

?>

<?php

$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbName, $con);

$result = mysql_query("SELECT * FROM $usertable");

$row = mysql_fetch_array($result);

$test = $row['propname'];
$beforepic = $row['beforepic'];
$afterpic = $row['afterpic'];

?>





<body>

<?php
echo "<table width='317' border='0' cellpadding='0'>";
while($row = mysql_fetch_array($result)){
echo "<tr>
    <td height='35' colspan='2'><p align='center' class='style1'>$test</p>    </td>
  </tr>";
echo "<tr>
    <td width='164' height='161'><img src='$beforepic' border='0' /></a></td>
    <td width='147'><img src='$afterpic' /></td>
  </tr>";
}
echo "</table>";
?>

</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/
Share on other sites

You are missing a row because you have a mysql_fetech_arary() after the mysql_query() which isn't doing much with the data except assigning them to variables. Remove it and assign the variables in the while loop.
[code]
<?php

$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbName, $con);

$result = mysql_query("SELECT * FROM $usertable");
?>
<body>

<?php
echo "<table width='317' border='0' cellpadding='0'>";
while($row = mysql_fetch_array($result)){

$test = $row['propname'];
$beforepic = $row['beforepic'];
$afterpic = $row['afterpic'];

echo "<tr>
    <td height='35' colspan='2'><p align='center' class='style1'>$test</p>    </td>
  </tr>";
echo "<tr>
    <td width='164' height='161'><img src='$beforepic' border='0' /></a></td>
    <td width='147'><img src='$afterpic' /></td>
  </tr>";
}
echo "</table>";
?>

</body>
</html>
[/code]
The problem is that you extract the data from the first record and set the the following variables:
[code]row = mysql_fetch_array($result);

$test = $row['propname'];
$beforepic = $row['beforepic'];
$afterpic = $row['afterpic'];[/code]

Then you loop through records 2 - 5 and display the variables from the 1st record each time.
[code]while($row = mysql_fetch_array($result)){
echo "<tr>
   <td height='35' colspan='2'><p align='center' class='style1'>$test</p>    </td>
 </tr>";
echo "<tr>
   <td width='164' height='161'><img src='$beforepic' border='0' /></a></td>
   <td width='147'><img src='$afterpic' /></td>
 </tr>";
}
[/code]

Try this:
[code]<?php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
-->
</style>
</head>

<?php

$hostname = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
$usertable = "bddtable";
$dbName = "epicr001";
$propname = $_POST['propname'];

?>

<?php

$con = mysql_connect($hostname,$username,$password);
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db($dbName, $con);

$result = mysql_query("SELECT * FROM $usertable");
?>

<body>

<?php
echo "<table width='317' border='0' cellpadding='0'>";
while($row = mysql_fetch_array($result)){
echo "<tr>
   <td height='35' colspan='2'><p align='center' class='style1'>$row['propname']</p>    </td>
 </tr>";
echo "<tr>
   <td width='164' height='161'><img src='$row['beforepic']' border='0' /></a></td>
   <td width='147'><img src='$row['afterpic']' /></td>
 </tr>";
}
echo "</table>";
?>

</body>
</html>
?>[/code]
Use single quotes for strings and concatenate with .
[code]
<?php
while($row = mysql_fetch_array($result)){
echo '<tr>
    <td height="35" colspan="2"><p align="center" class="style1">'.$row['propname'].'</p></td>
  </tr>';
echo '<tr>
    <td width="164" height="161"><img src="'.$row['beforepic'].'" border="0" /></a></td>
    <td width="147"><img src="'.$row['afterpic'].'" /></td>
  </tr>';
}
?>
[/code]

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.