Warning: mysql_fetch_array():

Discussion in 'PHP' started by HungryMinds, May 23, 2011.

  1. #1
    Hi...

    Unexpected Getting This Error When I'm Running This Code On My FTP Server, But On My Local PC, It's Working Perfect :S

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mypath/form.php on line 17

    
    $localhost    =    "localhost";
    $username =    "";
    $password    =    "";
    $database    =    "mydb";
    
    $conn = mysql_connect($localhost,$username,$password);
    
    if(!$conn){
        print "Unable to Connect DB";
    }
    if(!mysql_select_db($database, $conn)){
        print "Unable to select DB";
    }
    
    $Query = "Select * From Orders";
    $Result = mysql_query($Query, $conn);
    $Row = mysql_fetch_array($Result); // ERROR Is HERE
    $Id = $Row["Id"];
    
    Code (markup):
     
    HungryMinds, May 23, 2011 IP
  2. Sefrez

    Sefrez Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The code looks fine. The only problem I can think of is that mysql_query() is failing. Try adding a check statement to see if it errors out, or simply:
    
    $Result = mysql_query($Query, $conn) or die(mysql_error());
    
    PHP:
    Also, make sure the database &/or table(s) exist in the connection your making.
     
    Sefrez, May 23, 2011 IP
  3. mobileappdoctor

    mobileappdoctor Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I usually check to make sure it's a valid resource before i call fetch array because an empty result can also cause an error.

    if(is_resource($Result)) {
    $Row = mysql_fetch_array($Result);
    } else {
    print(mysql_num_rows($Result));
    print(mysql_error());
    }
     
    mobileappdoctor, May 23, 2011 IP
  4. m0nster

    m0nster Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    looks good to me, maybe you don't have your db set up correctly, use the "or die()" statement and see what it outputs
     
    m0nster, May 26, 2011 IP
  5. niks00789

    niks00789 Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #5
    see if this helps :

    
    $localhost    =    "localhost";
    $username =    "";
    $password    =    "";
    $database    =    "mydb";
    
    
    mysql_connect($localhost, $username, $password) or die(mysql_error());
    mysql_select_db($database) or die(mysql_error());
    
    $query = "Select * From Orders";
    $result = mysql_query($query);
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    {
        printf("ID: %s", $row["id"]);
    	//echo 'ID : ' . $row['id'] . '<br/>';
    }
    
    PHP:
     
    niks00789, May 27, 2011 IP
  6. piluan

    piluan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    $localhost = "localhost";
    $username = "root";
    $password = "";
    $database = "mydb";
     
    piluan, May 30, 2011 IP