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

Discussion in 'MySQL' started by cssclub, Sep 13, 2007.

  1. #1
    I am trying to pull a random record out of a database running MYSQL. I get the error code "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource". The code is
    <?php
    
    // Create the connection and select the DB
    $link = mysql_connect("localhost","user","password");
    
    if ($link) {
       mysql_selectdb("test",$link);
    
       // Select records from the DB
       $query  = "SELECT * FROM demo ORDER BY Rand() LIMIT 2";
       $result = mysql_query($query);
    
       // Display records from the table
       echo "<table border='1'>";
       while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
          echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
       }
       echo "</table>";
    } else {
        echo "Can't connect to the database!";
    }
    
    ?>
    PHP:
    Does anyone know whats happing?
     
    cssclub, Sep 13, 2007 IP
  2. jmhyer123

    jmhyer123 Peon

    Messages:
    542
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Usually when you get
    it means your info is wrong (table, db, etc.) or the result is empty (there is no data in the table) so check that.

    Also I don't know if it's just a typo or the problem but

    "mysql_selectdb" should be "mysql_select_db" (LINE 7)

    Hope that helps ;)
     
    jmhyer123, Sep 13, 2007 IP
  3. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can use this to debug also:
    
    $result = mysql_query($query);
    if( !$result ) {
      echo mysql_error();
    }
    
    PHP:
     
    hamidof, Sep 13, 2007 IP
  4. _Eugene_

    _Eugene_ Member

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    may be u have a field named Rand
     
    _Eugene_, Sep 14, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    RAND() is a function!

    The problem is most likely with mysql_selectdb() which should be mysql_select_db() - note the extra underscore
     
    krt, Sep 14, 2007 IP
  6. cssclub

    cssclub Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks All! I found someone that could help me in person, but he told me exactly the same stuff that you did! Your info will help with other problems I am sure!
     
    cssclub, Sep 30, 2007 IP
  7. moin1407

    moin1407 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi

    I know this is a very old thread. I have replied that for the all future visitors with a view to helping them:

    see below:

    $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    Cart is table name here. I know that when table name is too short and without underscore thn mql shows the error. because mysql can not detect short name. when table name is too short and without underscore thn you must have to add ` both side of the table name in query. Or if u dont want to add ` thn select a name with a underscore or select a long name.

    See example and try below one and both will work :

    $result = mysql_query("select count(*) from `cart` where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    or,

    $result = mysql_query("select count(*) from cart_1 where cookieId = '" . GetCartId() . "' and itemId = $itemId");
     
    moin1407, Nov 18, 2010 IP
  8. kevin5fan

    kevin5fan Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I am having the same problem and what's with the other 3 posts.
     
    kevin5fan, Nov 21, 2010 IP
  9. dlowe

    dlowe Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Also getting: PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    Code: while ($row = call_user_func($func_name,$res_id)){

    Any Suggestions?
     
    dlowe, Dec 20, 2010 IP
  10. dlowe

    dlowe Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Also getting: PHP Warning: Invalid argument supplied for foreach()

    Code:$r = DBquery($q);

    $out = array();

    foreach ($r as $item)

    {

    $id = $item['id'];

    Any Suggestions?
     
    dlowe, Dec 20, 2010 IP