NLT Posted May 14, 2013 Share Posted May 14, 2013 (edited) 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 May 14, 2013 by NLT Quote Link to comment https://forums.phpfreaks.com/topic/278005-mysqli-query/ Share on other sites More sharing options...
Solution requinix Posted May 14, 2013 Solution Share Posted May 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278005-mysqli-query/#findComment-1430086 Share on other sites More sharing options...
NLT Posted May 15, 2013 Author Share Posted May 15, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/278005-mysqli-query/#findComment-1430199 Share on other sites More sharing options...
Jessica Posted May 15, 2013 Share Posted May 15, 2013 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? Yes, and yes. Quote Link to comment https://forums.phpfreaks.com/topic/278005-mysqli-query/#findComment-1430207 Share on other sites More sharing options...
ginerjm Posted May 15, 2013 Share Posted May 15, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278005-mysqli-query/#findComment-1430214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.