Error caused by mysql selection

I am currently learning MySql, but ive hit this issue. The following code should just query db for everything that is in the users table. but he returns this error. Error: SELECT * FROM users , which doesn't help me at all. I can successfully insert an item into the database, but I cannot select it. I also tried $sql = "SELECT * FROM ama.users"; my db structure

  ama |-users 

any help would be greatly appreciated.

  $conn = new mysqli($_ENV['OPENSHIFT_MYSQL_DB_HOST'],$_ENV['OPENSHIFT_MYSQL_DB_USERNAME'], $_ENV['OPENSHIFT_MYSQL_DB_PASSWORD'], 'ama'); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $username = "Doe"; $password = "johnexample"; $sql = "SELECT * FROM users"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); 
+5
source share
2 answers

From the PHP manual :

mysqli :: query will return the object to success and return false on error.

Therefore, you can use it without checking the data type (===):

 if ($conn->query($sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } 

For a better understanding, you can use var_dump() and check what you get:

 var_dump($conn->query($sql)); 
+5
source

The documentation says:

Returns false on error. For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries, mysqli_query () will return a mysqli_result object. For other successful queries, mysqli_query () will return TRUE.

So do something like

 $result= $db->query($sql); 

and then check the lines in $ result

+1
source

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


All Articles