Jump to content

MySQLI query


NLT
Go to solution Solved by requinix,

Recommended Posts

Hey,
 
So recently I decided to try learn MySQLi but I can't find any in depth guides or tutorials so I'm pretty much trying things out for myself.
 
I'm having trouble with a simple query and I'm not sure why, it doesn't throw errors (even when I had an error in the query, I had to manually use $mysqli->error)
 
Here is the code: 

<?PHP
error_reporting(E_ALL);
$mysqli = new mysqli(); // removed data
 
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
 
 
$preparation = $mysqli->prepare("SELECT `username`,`password` FROM `users` WHERE `email` = ? ");
 
$username="test";
$preparation->bind_param('s', $username);
 
$Rusername = "";
$password = "";
 
 
$preparation->execute();
$preparation->bind_result($Rusername, $password);
while($preparation->fetch()) {
echo $Rusername . "<br>" . $password . "<br><br>";
}
$preparation->close();
?>

Question 2: Can I make it display any sort of error throughout any part of the code?

Edited by NLT
Link to comment
Share on other sites

  • Solution

1. Maybe it's just what you've posted but it appears you're trying to search for a user with the email address "test". Try without the WHERE condition.

2. You can make it check for errors anywhere you want. You should check a] after connecting, b] after preparing, and c] after executing.

Link to comment
Share on other sites

1. Maybe it's just what you've posted but it appears you're trying to search for a user with the email address "test". Try without the WHERE condition.

2. You can make it check for errors anywhere you want. You should check a] after connecting, b] after preparing, and c] after executing.

Okay that worked, but is there like a way I could of made that throw a error because no rows were found or would I have to check the amount of rows?

Link to comment
Share on other sites

To elaborate on the previous response:

 

Yes - you can make your user aware that there were no results found, although it is not 'technically' an error.  It may be a user (input) error in that the item sought was not found in the table, but it is not truly an error.

 

You should always check the results of a query for an error usually done with:

 

if (!$preparation->execute())

{

   (show some error message here)

  ( handle the failure)

}

else

{

  if ($preparation->num_rows ==0)

  {

   (handle no rows here)

  (handle the failure)

  }

//  continue on here if query was good and results were found

....

....

 

After validating the query ran, you can then check if there were any results.

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.