We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,449 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problem: expects parameter 1 to be resource

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

18
Contributors
31
Replies
3 Years
Discussion Span
4 Months Ago
Last Updated
45
Views
Question
Answered
Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 269
Skill Endorsements: 26

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?

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 269
Skill Endorsements: 26
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";
}
dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 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

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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?

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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

dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 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'";

dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 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

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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.

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 0

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

dasatti
Junior Poster in Training
57 posts since Dec 2008
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 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

Avasulthiris
Junior Poster in Training
56 posts since Jun 2009
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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!

scyfox
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

network18
Practically a Master Poster
629 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
Skill Endorsements: 0

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

dark.matter
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.......

jitu99
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

What if your query return an empty recordset?

vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

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 ;
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page generated in 0.1312 seconds using 2.74MB