显示在php中获取mysql数据的结果

I'm not sure if I doing the connection and query 100%, but since the error I'm getting is only with the results I'm assuming I am. I don't, however, know how to process the data correctly to display it.

php:

$query = SELECT * FROM `user` WHERE `email` = 'mflem@yahoo.com' AND 'password' = 'password'

$db = new mysqli('localhost','root','','xxxxxxx') or die('unable to connect!');
if($db->connect_errno){
   $message = $db->connect_error;
} else{
   if($result = $db->query($query)){
       $result->close();
       while($row = mysqli_fetch_assoc($result)){
          echo "result: " . $row['user_Id'];
       }
       return $result;
   } else{
       $message = $db->error;
       return $message;
   }
}
$db->close();

The error in the browser console it says.


Warning: mysqli_fetch_assoc(): Couldn't fetch mysqli_result in C:\xampp\htdocs\apps\MyVyn\Utils\utils\servConn.php on line 14

Notice: Undefined variable: return in C:\xampp\htdocs\apps\MyVyn\Utils\utils\servConn.php on line 36

Could someone point out what I'm missing? Also are there any better ways of fetching data from you DB and displaying the results?

Edit:

Me: "You're not selecting anything."

OP: "These is, I didn't notice i didn't include it."

Always post full code when posting. When something is omitted, it leaves room for confusioin.


You're not selecting anything.

Meaning, you need to select a column/columns or everything * from a database table.

Example:

$query = mysqli_query($db,"SELECT column FROM table_name");

or

$query = mysqli_query($db,"SELECT column_1, column_2 FROM table_name");

or

$query = mysqli_query($db,"SELECT * FROM table_name");

Yet, I suggest you look into use prepared statements, or PDO with prepared statements, there are many examples you can use there to base yourself on.


Edit: #2

You're closing your DB connection prematurely with $db->close(); in:

else{
   if($result = $db->query($query)){
       $result->close();
       while($row = mysqli_fetch_assoc($result)){
          echo "result: " . $row['user_Id'];
       }
       $db->close(); // <= there
       return $result;
   } else{
       $message = $db->error;
       $db->close(); // <= there
       return $message;
   }
}

Remove the $result->close(); from inside all your loops and place it outside of a successful query.