954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mysql_fetch_array problem

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 :)

Lizard
Newbie Poster
1 post since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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:
[PHP]
$sql = "SELECT * FROM users WHERE user='".$_SESSION['user']."'";
echo $sql;
$rs = mysql_fetch_array(mysql_query($sql));
[/PHP]
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.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You