wildkatana Posted May 12, 2008 Share Posted May 12, 2008 For some reason, this code is running the foreach loop twice for each cell. It is driving me crazy! You can see an example here: http://lwhitin.is2.byuh.edu/test/tv.php It is supposed to take the q argument which is the table name and then return the contents of that table. But somethings going wrong.... Please help if you can. Thanks! <?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'username', 'password'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $sql="SELECT * FROM $q"; $result = mysql_query($sql); echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; foreach ($row as $cell) {echo "<td>$cell</td>";} echo "</tr>\n"; } echo "</table>"; mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/105351-solved-help-foreach-and-mysql-and-arrays/ Share on other sites More sharing options...
hitman6003 Posted May 13, 2008 Share Posted May 13, 2008 mysql_fetch_array returns an array with both associative (i.e. string) keys and numerically indexed keys. Use mysql_fetch_assoc or mysql_fetch_row to get associative or numeric indexes. Link to comment https://forums.phpfreaks.com/topic/105351-solved-help-foreach-and-mysql-and-arrays/#findComment-539561 Share on other sites More sharing options...
wildkatana Posted May 13, 2008 Author Share Posted May 13, 2008 Thank you, changing to fetch row fixed it! Link to comment https://forums.phpfreaks.com/topic/105351-solved-help-foreach-and-mysql-and-arrays/#findComment-539566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.