SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    mysql_fetch_array error

    hi folks,
    i m new in php. so far i m doing good but now i get the error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in xxxxxx
    i seriously hate this error. i can't find any help on why this error come but here is my code
    Code PHP:
    function get_heads($cat_id){
        $query = mysql_query("select * from categories where menu_name = {$cat_id}");
    /*    if($result = mysql_fetch_array($query)){
            return $result;
        }else{
            return null;
        }*/
        while($result=mysql_fetch_array($query)){
            if(!$result){
                die (mysql_error());
            }
            echo "$result";
        }
        }
    first i thought i was doing the if statement wrong but now i tried the while and even then i have issues. help pls
    All those who wander aren't lost.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The error indicates that the query has ended with an error.
    You have to check that before starting the while loop. And $result is an array, so instead of echo try print_r:
    Code PHP:
    function get_heads($cat_id) {
        $query = "
          select * 
          from categories 
          where menu_name = {$cat_id}
        ";
        $result = mysql_query($query);
        if (!$result) {
           die (mysql_error() . " in query $query");
        }
        while ($row = mysql_fetch_array($result)) {
            print_r($row);
            echo "<br />";
        }
    }

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It usually means you had an error in your sql statement, so the smart move is to echo the error - OR go look in your mysql log file - pick up the exact query and check it.

    Here is a guess. If your menu_name is a string, not an integer, then you have failed to quote the string in your statement
    Code:
    ".....where menu_name = '$cat'";

  4. #4
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What Cups said will probably fix your problem, however if you run into this error again with a more complex query, or you just can't find what the problem is, what I always do is echo the query and throw it into phpmyadmin, see what that says.

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yeah, I should have said that too - what Kokos means is do this:
    PHP Code:
    function get_heads($cat_id){
        
    $qry "select * from categories where menu_name = {$cat_id}"
        
    $query mysql_query($qry);
    // etc
    // then if things go wrong add this line:
    echo $qry 
    Then you can see your query AND the variables that PHP has inserted for you (or not ) and then paste it into whatever you use to manage your database.

    Instead of doing that, you can look in your mysql logfile.

    As for finding your logs, my logs are in:

    "C:\Program Files\MySQL\MySQL Server 5.0\data"

    My logfile is usually that last file in that folder that has changed, go to the bottom of that file and you will see the last query mysql handled.

    The logfile you want is normally called <name of your machine>.log

    When it gets too long, delete it, and restart the mysql server in Services and it creates a new one.

    Maybe there is an easier way?

  6. #6
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    well now i did my query simple way and not in function so uncomplicated things for me until they get solved. but the error seem to be very deceiving. it says now You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 in query and here is my query
    Code PHP:
    $query = mysql_query("select * from categories where menu_name= {$sub_cat["id"]}");
    if(!$query){
     die (mysql_error() . " in query $query");
    }
    even i tried to wrap the query in single quote i get inner error around id.this query is driving me nuts by not working.
    Edit : I tried to figure out. and seem if i do a simple simple * from table it doesn't prompt any error but after where it give same error.but still wondering why its happening
    All those who wander aren't lost.

  7. #7
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nofel View Post
    well now i did my query simple way and not in function so uncomplicated things for me until they get solved. but the error seem to be very deceiving. it says now You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 in query and here is my query
    Code PHP:
    $query = mysql_query("select * from categories where menu_name= {$sub_cat["id"]}");
    if(!$query){
     die (mysql_error() . " in query $query");
    }
    even i tried to wrap the query in single quote i get inner error around id.this query is driving me nuts by not working.
    Edit : I tried to figure out. and seem if i do a simple simple * from table it doesn't prompt any error but after where it give same error.but still wondering why its happening
    You still haven't quoted your variable, isn't this causing the problem?

    Code MySQL:
    SELECT * FROM `categories` WHERE `menu_name` = '{$sub_cat["id"]}'

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  8. #8
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kokos View Post
    You still haven't quoted your variable, isn't this causing the problem?

    Code MySQL:
    SELECT * FROM `categories` WHERE `menu_name` = '{$sub_cat["id"]}'
    can u tell with the whole mysql_query thing with the query. i m confused here
    All those who wander aren't lost.

  9. #9
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query mysql_query("SELECT * FROM `categories` WHERE `menu_name` = '{$sub_cat["id"]}'");
    if(!
    $query){
     die (
    mysql_error() . " in query $query");


    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  10. #10
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah! now it worked. so where was i going wrong?
    All those who wander aren't lost.


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
  •