mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Discussion in 'PHP' started by zealot777, Nov 29, 2009.

  1. #1
    Hello,

    Can anyone help me on this problem? I'm getting this error (see title) and i'm sure that my query works fine cause i even tried it in phpmyadmin and this works very well in my local machine but when uploaded in my domain I already get that error. Any ideas? Thanks in advance!
     
    zealot777, Nov 29, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    show the query and code where the error occurs.
     
    shallowink, Nov 29, 2009 IP
  3. ColdMoon

    ColdMoon Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $query = mysql_query("SELECT * FROM table") or die(mysql_error());
    while ($data = mysql_fetch_array($query)) {
        // data processing goes here
    }
    PHP:
    This should work.
     
    ColdMoon, Nov 29, 2009 IP
  4. inanobot

    inanobot Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This function works when there is at least 1 result. In other words, in your query there is no result. One more thing, please remember to put ` ` at the start and end of every table name. Your query must be:
    $query = mysql_query("SELECT * FROM `table`") or die(mysql_error());
    while ($data = mysql_fetch_array($query)) {
        // data processing goes here
    }
    PHP:
     
    inanobot, Nov 30, 2009 IP
  5. htt5001

    htt5001 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Your query may return an empty result. To fix this, you need to check if the $query return false or empty:

    $query = mysql_query("SELECT * FROM `table`");
    if (!$query or mysql_num_rows($query))
    {
    echo "Empty";
    }
    else
    {
    while ($data = mysql_fetch_array($query)) {
    // data processing goes here
    }
    }
     
    htt5001, Nov 30, 2009 IP