SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

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

    Code:
    $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. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    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. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hey multichild,

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

    Quote Originally Posted by multichild View Post
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
    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. #4
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #6
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #7
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK is this close?

    Code:
    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. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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:
    Code:
    ;extension=php_mysqli.dll
    Then restart your webserver and you should be good to go.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •