hi folks,
i m new in php. so far i m doing good but now i get the error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in xxxxxx
i seriously hate this error. i can’t find any help on why this error come but here is my code
function get_heads($cat_id){
$query = mysql_query("select * from categories where menu_name = {$cat_id}");
/* if($result = mysql_fetch_array($query)){
return $result;
}else{
return null;
}*/
while($result=mysql_fetch_array($query)){
if(!$result){
die (mysql_error());
}
echo "$result";
}
}
first i thought i was doing the if statement wrong but now i tried the while and even then i have issues. help pls
The error indicates that the query has ended with an error.
You have to check that before starting the while loop. And $result is an array, so instead of echo try print_r:
function get_heads($cat_id) {
$query = "
select *
from categories
where menu_name = {$cat_id}
";
$result = mysql_query($query);
if (!$result) {
die (mysql_error() . " in query $query");
}
while ($row = mysql_fetch_array($result)) {
print_r($row);
echo "<br />";
}
}
It usually means you had an error in your sql statement, so the smart move is to echo the error - OR go look in your mysql log file - pick up the exact query and check it.
Here is a guess. If your menu_name is a string, not an integer, then you have failed to quote the string in your statement
What Cups said will probably fix your problem, however if you run into this error again with a more complex query, or you just can’t find what the problem is, what I always do is echo the query and throw it into phpmyadmin, see what that says.
Yeah, I should have said that too - what Kokos means is do this:
function get_heads($cat_id){
$qry = "select * from categories where menu_name = {$cat_id}"
$query = mysql_query($qry);
// etc
// then if things go wrong add this line:
echo $qry ;
Then you can see your query AND the variables that PHP has inserted for you (or not ;)) and then paste it into whatever you use to manage your database.
Instead of doing that, you can look in your mysql logfile.
As for finding your logs, my logs are in:
“C:\Program Files\MySQL\MySQL Server 5.0\data”
My logfile is usually that last file in that folder that has changed, go to the bottom of that file and you will see the last query mysql handled.
The logfile you want is normally called <name of your machine>.log
When it gets too long, delete it, and restart the mysql server in Services and it creates a new one.
well now i did my query simple way and not in function so uncomplicated things for me until they get solved. but the error seem to be very deceiving. it says now 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 ‘’ at line 1 in query and here is my query
$query = mysql_query("select * from categories where menu_name= {$sub_cat["id"]}");
if(!$query){
die (mysql_error() . " in query $query");
}
even i tried to wrap the query in single quote i get inner error around id.this query is driving me nuts by not working.
Edit : I tried to figure out. and seem if i do a simple simple * from table it doesn’t prompt any error but after where it give same error.but still wondering why its happening