1,105,295 Community Members

Problem: expects parameter 1 to be resource

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all,

I've been hitting the same problem over and over in my development. Sometimes when i do a MySQL query I keep getting the error
Warning: mysql_fetch_row() expects parameter 1 to be resource.

I don't understand why. What does this error mean, am I doing something wrong? I've googled it but I just get pages with the same error...

I'm using php 5.3, it's definitely not my mysql statement. I'm lost >.<

I even simplify my query code and still the same problem. The general code look something like this:

require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
$row = mysql_fetch_row ($checkresults);

It crashes on the last line :S

Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 

Take a look at the FAQ that is stickied, follow those instructions and then if it still doesn't work repost your code along with the new errors

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Still cant find anything. What do you mean by stickied?

The code is literally as simple as what I have above in my original thread. I'm just trying to understand the error.

It's been popping up everywhere. Even with the most simple code. It's unhappy with the mysql_fetch_row command.

Sometimes it works if i copy the code into a new file. I thought I might be using reserved words in mysql, but i'm definitely not. The database also has nothing weird in. It's just being weird.

And I have to finish this system by tomorrow night >.<
*is stressing*

What does that error mean? How is $checkresults not a resource?

Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 
require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
var_dump($checkresults);
die();
$row = mysql_fetch_row ($checkresults);

Do that and post the output

Member Avatar
dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 
require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
$row = mysql_fetch_row ($checkresults);require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
if($checkresults && mysql_num_rows($checkresults)>0)
{
$row = mysql_fetch_row ($checkresults);
}
else
{
if(!$checkresults)
echo "Error in query: ".mysql_error();
else
echo "No record Found";
}
Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I changed my code entirely throughout the night and now that specific problem is gone.

I do still occasionally get the same error, I just dont have an example of it right now. When i printf the error it comes out blank. It's been boggling me for a while.

I will post an example as soon as I run into it again

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Here we go, its happening in 4 different places now:

This is the error:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\wamp\www\EIRMSv2\pages\upload_rename_ac.php on line 135

This is they code in that section(lines 130-135):

require "dbconn.php";
			
			$checkquery = "SELECT * FROM flowchart WHERE id = '$id'";
			$checkresults = mysql_query($checkquery);
			
			$row = mysql_fetch_row ($checkresults);

My dbconn file definitely works. My values are posted correctly...

If i do a var_dump it returns bool(false). I dont know what that means?

Member Avatar
dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

You must use if($checkresults) check out side the statement $row = mysql_fetch_row ($checkresults);

$checkquery = "SELECT * FROM flowchart WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
if($checkresults)
{
	$row = mysql_fetch_row ($checkresults);
}

The reason is when it encounters some invalid $id, the mysql_query() function does not return valid result set. You can use mysql_error() to find out the reason why returned resultset is not valid

Member Avatar
dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

One of the reasons that invalid result set is returned could be you are comparing a string value with an integer type field in database.

Make sure that if the id field in flowchart table is int then use this statement $checkquery = "SELECT * FROM flowchart WHERE id = $id"; instead of $checkquery = "SELECT * FROM flowchart WHERE id = '$id'";

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Another module of the project im working on is mysqli instead of mysql. Could it be that that is causing the problems? That module uses its own dbconn file though so i dunno why its having this issue

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

hi dassati,

i tried that, that is not the problem unfortunately. if only it was so simple. the sql runs in phpmyadmin. the single quotes don't matter.

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

it is not the $id. the code is set to always give a valid id. problem is still there if i say $id=1;

that is also not the problem. im pretty sure its compatibility issues between mysql and mysqli both selecting the database, even if its in different places. I dont think the server likes it when i select the same database two different ways. I just need someone who understands this to explain it to me

Member Avatar
dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

make sure that close mysqli connection before playing with simple mysql and vise versa

Member Avatar
Avasulthiris
Junior Poster in Training
54 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thats exactly what I plan on doing thanks. I hope thats the problem though, I havn't really seen this before. It's a lot of work to go change all the queries in the whole project >.< haha

I will keep looking to see if it comes up again

thanks

Member Avatar
scyfox
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
$row = mysql_fetch_row ($checkresults);

It crashes on the last line :S

As Daft Punk says... harder, better, faster, stronger.... try this.

require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery);
if (!empty($checkresults)) {
       $row = mysql_fetch_row ($checkresults);
} else {
   echo "Issues trying to fetch results. Check the error-->" . mysql_error();
die();
}

If Empty checks if the array comes with some data no matter if it is boolean or not, so if the query is wrong or you ain't got nothing inside de bd, it skips the "fetch_array" part and goes straight to the error handling part.

Really useful and clutter free.

Silla!

Member Avatar
network18
Practically a Master Poster
643 posts since Sep 2009
Reputation Points: 15 [?]
Q&As Helped to Solve: 78 [?]
Skill Endorsements: 0 [?]
 
0
 

no absolutely not, mysqli is just the improved extension of mysql set of functions.It should not make any harms.
Also post your code dbconn.php for others to see, maybe it needs some improvements.

Member Avatar
dark.matter
Newbie Poster
1 post since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

i got this error when my SQL statement was incorrect. The field names were incorrectly spelled.

Member Avatar
jitu99
Newbie Poster
1 post since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

well i was getting the same error............in my case the spelling of the column name in my code and the database was different...the colum name in the database was tilte and in the code it was title....so i had to change the column name in the database to title.......

Member Avatar
vaultdweller123
Posting Pro
577 posts since Sep 2009
Reputation Points: 32 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 2 [?]
 
0
 

What if your query return an empty recordset?

Member Avatar
JRM
Practically a Master Poster
618 posts since Nov 2006
Reputation Points: 107 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

One of the reasons that invalid result set is returned could be you are comparing a string value with an integer type field in database.

Make sure that if the id field in flowchart table is int then use this statement $checkquery = "SELECT * FROM flowchart WHERE id = $id"; instead of $checkquery = "SELECT * FROM flowchart WHERE id = '$id'";

I think you are on the right track, but the variable needs to be appended like so:

$checkquery = "SELECT * FROM flowchart WHERE id = ". $id ;
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article