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

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

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?

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

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";
}

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

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?

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

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'";

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

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.

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

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

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

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!

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.

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

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.......

What if your query return an empty recordset?

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 ;

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

$checkquery = "SELECT * FROM flowchart WHERE id = ". $id ;

Yes I like to do in this way but the other will also work fine. You can try it.

change this:
$checkquery = "SELECT * FROM flowchart WHERE id = '$id'";
into this:
$checkquery = "SELECT * FROM `flowchart` WHERE `id` = '".$id."'";
so that you separate SQL from PHP code. and don't forget the grave accent on the table and field names

Original poster hasn't posted in this thread since Sep 4th, 2009. Also, Avasulthiris stated here that the project HAD to be finished by Sep 4th, 2009.

I don't think it is going to get marked "Resolved", and I'm pretty sure he isn't coming back to this thread anymore. :)

Just my 2¢.

If the query doesn't return anything, php will not instantiate the $result variable and php complains about expecting a boolean variable.

if (!$result) {
 // do something.
}

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\dijuv\student_express_entry.php on line 127 on
$sql =$sql = "INSERT INTO tblexpressstudent(zoneid, ";
$sql = $sql . "First_Name, Last_Name, Email, Address, ";
$sql = $sql . "City, State, Zip_Code, Phone_number, Work_Number, Alternate_Number, dob_day, ";
$sql = $sql . "dob_month, dob_year, ";
$sql = $sql . "System_Number, ";
$sql = $sql . "attending_school, level_of_education, credits, credit_outside, credit_transfer, lookingforsubject,";
$sql = $sql . "levelofdegree, enroll_within_three_month, permanent_resident, ";
$sql = $sql . "Graduation_year, internet_access, US_Military, Military_Benfits, ";
$sql = $sql . "WorkedFull, UniversityOffered, SecondaryUniversity, Coments, Acceptance, ";
$sql = $sql . "strStatus, ";
$sql = $sql . "dtCreated, dtModified, StrCreatedUNM, strModifiedUNM, ysnDeleted) ";
$sql = $sql . "VALUES('" . $zoneid . "',";
$sql = $sql . "'" . $first_name . "', '" . $last_name . "', '" . $email . "', ";
$sql = $sql . "'" . $address . "', '" . $city . "', '" . $state . "', " . $zip . ", ";
$sql = $sql . "" . $PhoneNo . ", '" . $WorkNo . "', '" . $AlternateNo . "', ";
$sql = $sql . "" . $dob_day . ", " . $dob_month . ", " . $dob_year . ", ";
$sql = $sql . "" . $SystemNumber . ", ";
$sql = $sql . "'" . $AttendingAnySchool . "', '" . $LevelOfEducation . "', '" .$Credits ."', '" .$CreditOutside . "', '" .$CreditTransfer ."' , ";
$sql = $sql . "'" . $LookingForSubject ."' , '" .$LevelOfDegree ."' , '" . $Enroll . "', ";
$sql = $sql . "'" . $PermanentResident . "', " . $GraduationYear . ", '" . $InternetAccess . "', ";
$sql = $sql . "'" . $USMilitary . "', '" . $MilitaryBenfits . "', '" . $WorkedFull . "', ";
$sql = $sql . "'" . $UniversityOffered . "', '" . $SecondaryUniversity . "', '" . $Coments . "', ";
$sql = $sql . "'" . $Acceptance . "', ";
$sql = $sql . "'" . $strStatus . "', '" . $date . "', '" . $date . "', ";
$sql = $sql . "'" . $Sunam . "', '" . $Sunam . "', '')";

No i'm still here.

I still occasionally get this and I still don't understand why. Often it's a misspelled name or quotations where they are needed. But the rest of the time its just for no reason what so ever. I end up recoding what ever im working on until it works.

I'm assuming its just a php configuration issue, because i've only seen it happen so frequently on my one laptop. It works better on the other computer, so I'm just going to solve the thread. Still unsure though.

Good luck if you get this problem

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

Take the code and do this:

require "dbconn.php";
$id=$_POST['id'];
$checkquery = "SELECT * FROM table WHERE id = '$id'";
$checkresults = mysql_query($checkquery) or die(mysql_error());
//this should show an error if it occurs
$row = mysql_fetch_row ($checkresults);

when you get the error, post it back here, though I think, the problem is that either the column name or the table name is wrongly spelt, but the query itself looks fine and let the name of the table be in backticks like `table` incase you're using a reserved word in SQL

Just take table name:

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

Attention: ` != ' :)
Hope it'll help.

i used this piece of code to fix the error... this should fix yours...

mysql_select_db('database');

the reason is you cannot connect to your database.. that simple

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.