MySQL doesn't get the right data? (Php)

This is pretty straight forward. EDIT: Updated question and added fourth echo.

Here is the PHP code:

<?php $ratings="3"; $item="Inception"; $query="SELECT * FROM items WHERE item = '". $item ."' LIMIT 1"; echo $query; echo "<br />"; $result=mysql_query($query); echo $result; echo "<br />"; while ($row = mysql_fetch_array($result)) { $item_id = $row['item_id']; echo $item_id; echo "<br />"; } $query_two = "INSERT INTO ratings (rating, item_id), VALUES (' {$ratings} ', ' {$item_id} ')"; echo $query_two; $sql = mysql_query($query_two); mysql_close(); ?> 

Here is the web output with all the echoes:

 SELECT * FROM items WHERE item = 'Inception' LIMIT 1 Resource id #7 INSERT INTO ratings (rating, item_id), VALUES (' 3 ', ' ') 

Why is my $ item_id empty? (third line under the resource identifier)

+6
source share
6 answers

This piece of code produces it:

 $result=mysql_query($query); echo $result; 

It shows Resource... because it has a resource type, it’s just a kind of special handler for the request, it doesn’t look like a regular type (string or int for example), so it has nothing to read for printing. A.

If you want to print data from a query, you must first extract it.

Also note that those mysql_* functions are mysql_* ; they are not recommended for use. Note from php manual:

Using this extension is not recommended. Instead, MySQLi or PDO_MySQL should use the extension. See Also MySQL: Choosing an API Guide and Frequently Asked Questions for more information. Alternatives to this feature include:

mysqli_query ()

PDO :: request ()

+5
source

This has nothing to do with identifiers from the database.

This ( Result#7 ) says that this result resource is the seventh resource created by your php script execution.

+4
source

Besides

 $query_two = "INSERT INTO ratings (rating, item_id), VALUES (' {$ratings} ', ' {$item_id} ')"; 

it should be

 $query_two = "INSERT INTO ratings (rating, item_id) VALUES (' {$ratings} ', ' {$item_id} ')"; 

You have a comma before VALUES .

Also, it seems that $item_id empty. Check if you have data for item = 'Inception' .

Regarding Result#7 follow the other answers.

+3
source

The resource identifier comes from the actual process / object, which is a MySQL query.

To return the desired result:

 $row = mysql_fetch_array( $query ); echo $row['item'] 
+2
source

You need to do something with the result resource. Try the following:

 $result=mysql_query($query); //echo $result; $result_array = mysql_fetch_assoc( $result ); print_r( $result_array ); 

EDIT: I see that you updated your question.

You must run your query item='Inception' directly in MySQL to confirm that the results are expected.

+2
source

You cannot repeat the result. You should get the result, for example, for an array:

 while ($row = mysql_fetch_array($query)) { echo $row['a_column'] . "<br />"; } 

or object:

 while ($variable = mysql_fetch_object($query) { $value = $variable->a_column; } echo $value; 

There are more ways, but these are just two examples.

+1
source

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


All Articles