Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Resource id #3 Error in MySql

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 die(mysql_error()); 
mysql_select_db("treehouse_movie_db") or die(mysql_error()); 
$data = mysql_query("SELECT * FROM movies") 
or die(mysql_error()); 
echo $data;
like image 838
user2533901 Avatar asked Jan 14 '23 00:01

user2533901


1 Answers

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

like image 70
alwaysLearn Avatar answered Jan 19 '23 04:01

alwaysLearn