Hi I’m new to PHP and MySQL and I’m trying to return a MySQL query using a PHP variable that has been passed to it. I currently get this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\moodle\logicquiz\quiz.php on line 57

Error performing query: 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 ''venn'' at line 1

Where "venn is the table name"

The code works when I put a table name in instead of $quiz_name, so I'm guessing there is a problem with my syntax as the error message suggests, any suggestions as to what I'm doing wrong would be gratefully appreciated.

Many Thanks
NH

//gets quiz name from URL passed in code in previous page
$quiz_name = $_GET['quiz'];


//counts number of questions in a quiz table
$result = mysql_query("SELECT * FROM '$quiz_name'");
$num_rows = mysql_num_rows($result12);
if (!$result) {
exit ('<p> Error performing query: ' .
	mysql_error() . '<p/>');
}

$result = mysql_query("SELECT * FROM '$quiz_name'");
$num_rows = mysql_num_rows($result12);

$result12 should be $result.

sorry I posted my code wrong

Even when I change it it still gets the same error, my actual code is

//gets quiz name from URL passed in code in previous page
$quiz_name = $_GET['quiz'];
//counts number of questions in a quiz table
$result2 = mysql_query("SELECT * FROM '$quiz_name'");
$num_rows = mysql_num_rows($result2);
if (!$result2) {
exit ('<p> Error performaing query: ' .
	mysql_error() . '<p/>');
}

NH

Do you have the database connection in your script ? And Check if $quiz_name isn't empty.

At the top of my code above all this is a database connection. When I run the code it will echo the variable $quiz_name with the correct value. so I would guess that means it's not empty. Also whenn I replace $quiz_name with venn (the table name) it runs fine, so I assume it's connecting to the database fine.

Thanks for your help on this.

//gets quiz name from URL passed in code in previous page
$quiz_name = $_GET['quiz'];
//counts number of questions in a quiz table
$result2 = mysql_query("SELECT * FROM '$quiz_name'");
$num_rows = mysql_num_rows($result2);
if (!$result2) {
exit ('<p> Error performing query: ' .
	mysql_error() . '<p/>');

echo '<table border="1" align="center">';
echo '<tr>';
echo '<td>';
echo '<h1>Quiz Name:</h1>';
echo '</td>';
echo '<td>';
echo '<h2>' .$quiz_name. '</h2>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo '<h1>Number of Questions:</h1>';
echo '</td>';
echo '<td>';
echo '<h2>' .$num_rows. '</h2>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo '</td>';
echo '<form action="quiz.php'.$quiz_name.'" method="get">';
echo '<td>';
echo '<input type="submit" value="Start Quiz"';
echo '</td>';
echo '</form>';
echo '</tr>';

echo '</table>';
?>

Try this.

mysql_query("SELECT * FROM $quiz_name");

If this doesn't work, print out the query and run it in phpmyadmin/mysql console. See what the problem is.

Thank you very much for your help. That solved it!

This is the code that works

$result2 = mysql_query("SELECT * FROM $quiz_name");
$num_rows = @mysql_num_rows($result2);
if (!$result2) {
exit ('<p> Error performing query: ' .
mysql_error() . '<p/>');}

Many thanks once again
:)

This question has already been answered. Start a new discussion instead.