Getting Resource id #3 Error in MySql

后端 未结 2 1840
抹茶落季
抹茶落季 2021-01-13 12:35

I ran this code and I got a Resource id #3 error where it should have showed the full movies table.

mysql_connect(\"localhost\", \"root\", \"password\") or d         


        
相关标签:
2条回答
  • 2021-01-13 13:02

    You're not getting an error, the MySQL API is just doing what you're asking it to: echoing the contents of $data, which is a MySQL query resource at this point. Extend the code to actually retrieve the results:

    while($row = mysql_fetch_object($data))
        var_dump($row);
    

    And you'll see the output.

    Note that the mysql_* API is deprecated since PHP 5.5 by the way.

    0 讨论(0)
  • 2021-01-13 13:13

    This is not an error Your query is getting executed and you are getting appropriate resource from mysql_query() as it should be returned.

    To get the response you have to use mysql_fetch_array() or mysql_fetch_assoc()

    mysql_connect("localhost", "root", "password") or die(mysql_error()); 
    mysql_select_db("treehouse_movie_db") or die(mysql_error()); 
    $data = mysql_query("SELECT * FROM movies") 
    or die(mysql_error()); 
    
    while($row = mysql_fetch_assoc($data))
    {
       print_r($row);
    }
    

    SUGGESTION: mysql_* are no longer maintained .Try switching to mysqli_* or PDO

    0 讨论(0)
提交回复
热议问题