mysql_fetch_array problem

Reply

Join Date: Mar 2006
Posts: 1
Reputation: Lizard is an unknown quantity at this point 
Solved Threads: 0
Lizard Lizard is offline Offline
Newbie Poster

mysql_fetch_array problem

 
0
  #1
Mar 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: mysql_fetch_array problem

 
0
  #2
Mar 13th, 2006
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 Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC