mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in


#1

I just cant work out what the problem is with below:

$queryt = mysql_query("select TOP 1 * FROM tbl_hotels Order By Id_Hot");
$gquery = mysql_fetch_assoc($queryt);
$administration = $gquery['Id_Hot'];
echo $administration; 


Keep getting this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in


#2

Did you make a connection to the database? http://www.php.net/manual/en/function.mysql-connect.php

Side note. Please don't use mysql_, it's deprecated, old, and crappy. Use PDO (http://www.php.net/manual/en/ref.pdo-mysql.php) or mysqli_(http://php.net/manual/en/book.mysqli.php)


#3

Hey multichild,

This one is actually very easy to find the answer for.

This error message is telling you that $queryt contains a boolean value (i.e. true or false) rather than a resource. As this is the output from mysql_query, see what the manual says about the return values of that function:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

So there must be a problem with your query.

If you google "mysql top" you'll see that MySQL doesn't support the TOP clause.. you need to use LIMIT instead.

Edit: I second what K. Wolfe says about not using the mysql extension.. I'm sure it's been mentioned before (god knows I probably tell someone here on the forum at least once a day) but you will end up getting bitten, as the mysql extension is going to be removed from PHP.


#4

Right ye thanks, will make the change.

And yes its not a connection problem, as there loads more connections above and below which work fine.


#5

OK I didnt realise TOp wasnt supported so didnt go looking for that in google in honesty, was trying to find the error elsewhere.

Thanks though


#6

I had a look at that PDO link and its very comprehensive for people trying to learn on the fly as I'm doing, each project I do is in a way an extension on the previous one with changes and improvements, so its hard to find this info out when your on your own in an office with no one to turn to but this forum.

What 'Im trying to say is that I dont really understand the PDO link properly, so am going to have to spend some time on it and see what the changes mean and what changes to do.

Every day is a learning curve, some days it feels like Im getting there then the next there another huge step to take and not a lot of time to learn it thoroughly.

The mysqli_* is very different too isnt it, am going to try and work out the change I need for the above script.

This forum is awesome though, the users are fantastic.


#7

OK is this close?

if ($result = mysqli_query("SELECT Id_Hot FROM tbl_hotels LIMIT 1")) {$administration = $result['Id_Hot'];
echo $administration;
}

Getting this error, so is it a server issue? Or am I getting it wrong somewhere

Fatal error: Call to undefined function mysqli_query() in

is the if statement correct before I call the query


#8

It sounds like you don't have the mysqli extension enabled. Open your php.ini file and uncomment the following line by removing the leading semi-colon:

;extension=php_mysqli.dll

Then restart your webserver and you should be good to go.


#9