Hello.

I have the following errors:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/winnie/public_html/pqQuiz/lib/templates.php on line 13

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/winnie/public_html/pqQuiz/lib/templates.php on line 13

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/winnie/public_html/pqQuiz/lib/templates.php on line 13

Lines 1-16 of templates.php shows the following:

function loadTemplates() {
global $templates;
if( !isset($_SESSION['user']) || $_SESSION['user'] == "" ){
    $selected = "templates";
}
else{
    $selected = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE user='".$_SESSION['user']."'"));
    $selected = $selected['template'];      
}
$loadTemplates = mysql_query("SELECT * FROM $selected");
while($loadTemplates = mysql_fetch_array($loadTemplates)){
    $templates[$loadTemplates['name']] = $loadTemplates['code'];
}
}

Could anyone help me solve this problem?

Thanks :)

Edited 3 Years Ago by pritaeas: Fixed formatting

The problem is exactly what the error message states. The resource is not a valid MySql resource. This is because the query is invalid or not producing any data.

Instead of building your SQL statement inside the mysql_query statement directly, build it in a variable then use that variable in the mysql_query statement. This allows you to echo out the contents of the $sql variable.

So do something like this:

$sql = "SELECT * FROM users WHERE user='".$_SESSION['user']."'";
echo $sql;
$rs = mysql_fetch_array(mysql_query($sql));

The echo statement will display the SQL that is being passed to your database. If you don't see any obvious errors in the statement, then copy and paste that query in a tool like phpMyAdmin's query tool to see what happens when you try to execute the statement.

What I'm suggesting is troubleshooting 101. Break the code down into one line at a time. Make sure each line is working before moving onto the next. If a line causes an error, break that line down into the smallest parts possible and test each aspect of the line. With PHP, your #1 debugging tool is the echo or print statements. Simply echo out variables and such to find what is happening.

This article has been dead for over six months. Start a new discussion instead.