Jacob Blue
New member
I am trying to run a script which will display the contents of a database table. You select which table via a radio button. I keep getting this error.
"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1"
If I do not check for the error the code will run and display the table but If I do not select a table it will produce the following statements.
Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource on line 33
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 39
My Code:
$selectedRadio = $_POST[options];
$sql = "SELECT * FROM '$selectedRadio'";
//$q = $db->query($sql) or die("SELECT Error: ".mysql_error());
$result = mysql_query($sql); //or die ('Error: '.mysql_error ());
//$r = $q->numRows();
echo "<table width=600 border=1>\n";
$i = 0;
while ($i < mysql_num_fields($result)) {
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "</tr>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "<tr>";
foreach ($row as $data)
echo "<td>$data</td>";
echo "</tr>";
}
echo "</table>\n";
echo "Table not displayed";
Does anyone know what I'm doing wrong?
"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1"
If I do not check for the error the code will run and display the table but If I do not select a table it will produce the following statements.
Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource on line 33
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 39
My Code:
$selectedRadio = $_POST[options];
$sql = "SELECT * FROM '$selectedRadio'";
//$q = $db->query($sql) or die("SELECT Error: ".mysql_error());
$result = mysql_query($sql); //or die ('Error: '.mysql_error ());
//$r = $q->numRows();
echo "<table width=600 border=1>\n";
$i = 0;
while ($i < mysql_num_fields($result)) {
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "</tr>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "<tr>";
foreach ($row as $data)
echo "<td>$data</td>";
echo "</tr>";
}
echo "</table>\n";
echo "Table not displayed";
Does anyone know what I'm doing wrong?