| | |
mysql_fetch_array problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2006
Posts: 1
Reputation:
Solved Threads: 0
Hello.
I have the following errors:
Lines 1-16 of templates.php shows the following:
Could anyone help me solve this problem?
Thanks
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
•
•
•
•
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
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.
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.
![]() |
Similar Threads
- Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource (PHP)
- mysql_fetch_array(): supplied argument is not a valid MySQL (PHP)
- mysql_fetch_array random problem? (PHP)
- PHP + MySQL Problem (PHP)
- search and results problem (PHP)
- Problem with foreach when using linking (PHP)
Other Threads in the PHP Forum
- Previous Thread: wht is HTML,PHP,CGI(Common Gateway Interface)
- Next Thread: need help with a php issue
| Thread Tools | Search this Thread |
apache api array auto beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod host href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql problem query radio random recursion recursiveloop remote script search select server sessions sms snippet source space sql static survey syntax system table tutorial up-to-date update upload url validator variable video web youtube





