Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error produced from mysql select

Tags:

php

mysql

I'm currently learning MySql, but ive hit this problem. the following code should just query the db for everything in the users table. but it returns this error. Error: SELECT * FROM users which helps me not at all. I am able to successfully insert an item into the database, but I am unable to select from it. I've also tried $sql = "SELECT * FROM ama.users"; my DB structure is

 ama
 |-users

any help would be much 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();
like image 228
hurnhu Avatar asked Feb 05 '16 06:02

hurnhu


1 Answers

From the PHP Manual:

mysqli::query will return object in success and return false in failure.

So you can use it without checking data type (===):

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

For more better understanding you can use var_dump() and check what are you getting like:

var_dump($conn->query($sql));
like image 159
devpro Avatar answered Sep 21 '22 10:09

devpro