0

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?

Stephen Adkins
  • 65
  • 1
  • 1
  • 6
  • You're not selecting anything. => http://dev.mysql.com/doc/refman/5.0/en/select.html – Funk Forty Niner Aug 20 '14 at 20:02
  • These is, I didn't notice i didn't include it. – Stephen Adkins Aug 20 '14 at 20:11
  • I suggest you add error reporting to your file(s) then. Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` see if it yields anything. Add that right after your opening ` – Funk Forty Niner Aug 20 '14 at 20:12
  • I've never use mysqli I am a PDO person but is it normal to `$result->close();` before you do anything? – Sebastien Aug 20 '14 at 20:13
  • This Q&A http://stackoverflow.com/q/2879500/ will explain it better. – Funk Forty Niner Aug 20 '14 at 20:14
  • @Fred-ii- Well I get what was discuss in the previously stated Stack Q&A but it does seems like the OP is closing `$result` before the while loop that seems to be 'getting' is data from the DB that is why I asked the question. – Sebastien Aug 20 '14 at 20:19
  • @Sebastien Yes, you're right. – Funk Forty Niner Aug 20 '14 at 20:25
  • @StephenAdkins You have an error, do show us what it is, while placing your query in your code, instead of on top of your question like that. That little detail is important. Plus, as Sebastien noted, you're closing prematurely in your 2nd `if` condition. – Funk Forty Niner Aug 20 '14 at 20:26

1 Answers1

0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141