cjbeck71081 Posted December 20, 2006 Share Posted December 20, 2006 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.phphere 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><?phpecho "<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 More sharing options...
esukf Posted December 20, 2006 Share Posted December 20, 2006 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><?phpecho "<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] Link to comment https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/#findComment-145254 Share on other sites More sharing options...
Round Posted December 20, 2006 Share Posted December 20, 2006 try a while loopwhile ($row = mysql_fetch_array($result)){$test = $row['propname'];$beforepic = $row['beforepic'];$afterpic = $row['afterpic'];} Link to comment https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/#findComment-145255 Share on other sites More sharing options...
Psycho Posted December 20, 2006 Share Posted December 20, 2006 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><?phpecho "<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] Link to comment https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/#findComment-145256 Share on other sites More sharing options...
cjbeck71081 Posted December 20, 2006 Author Share Posted December 20, 2006 I tried that last suggestion and it didnt work, says there is something wrong with the code "...>$row['propname']</p..."Any suggestion? Link to comment https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/#findComment-145262 Share on other sites More sharing options...
esukf Posted December 20, 2006 Share Posted December 20, 2006 Use single quotes for strings and concatenate with .[code]<?phpwhile($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] Link to comment https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/#findComment-145266 Share on other sites More sharing options...
cjbeck71081 Posted December 20, 2006 Author Share Posted December 20, 2006 suggested answer by "esukf" worked perfect, thanks dude.Problem Solved. Link to comment https://forums.phpfreaks.com/topic/31379-problem-with-fetch-array/#findComment-145267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.