Jump to content

[SOLVED] Help! Foreach and mySQl and Arrays


wildkatana

Recommended Posts

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);
?>

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.