Getting Resource ID # 3 Error in MySql

I ran this code, and I got a resource identifier error number 3, where it was supposed to show a full table of films.

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; 
+4
source share
2 answers

This is not an error Your query is executed, and you get the corresponding resource from mysql_query() , since it must be returned.

To get the answer you should 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); } 

OFFER: mysql_ * are no longer supported. Go to mysqli_ * or PDO

+9
source

You are not getting an error, the MySQL API just does what you ask: repeat the contents of $data , which is the MySQL query resource at this point. Extend your code to really get results:

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

And you will see the result.

Note that the mysql_ * API has been deprecated since PHP 5.5 .

0
source

Source: https://habr.com/ru/post/1489199/


All Articles