Mhari Posted March 2, 2017 Posted March 2, 2017 Hello i am still wondering about my code that always show same errorafter i am made some changes.actually i am run the codes in my localhost in my android phonewith version: php 5.4 and MySQL 5.1and then will be uploaded to my helio account.(coding with text editor more easy than with ftp site i think ) this is my code if i am run this code: <?php $db = new mysqli("localhost", "root", "", "default_db"); if ($db->connect_error) { die("Connection error"); } else { $sql = "SELECT * FROM 'users' WHERE 'nick' = '$u'"; if ($db->query($sql) === TRUE) { $data = $db->query($sql)->fetch_array(); echo $data['email']; } else { echo "Query error: " . $db->error; } } ?> it will show an error like this: Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'nick' = ''' at line 1 please anyone tell me which the part that wrongi am confuse for the last some days up to now because this Thanks in advance..
wolstech Posted March 2, 2017 Posted March 2, 2017 SELECT * FROM 'users' WHERE 'nick' = '$u' Should be: SELECT * FROM users WHERE `nick` = '$u' You used the wrong type of quotes. You need the ` symbol (the one to the left of the number 1, above the tab key), not the ' (apostrophe). around the field names. If you want something around the table names, you should use these there too, though the code will work without anything around the table name.
Mhari Posted March 2, 2017 Author Posted March 2, 2017 Hi sirI have tried with the code: $sql = "SELECT * FROM users WHERE `nick` = 'Mhari'"; I got the same result that is error but with no error message.I also tried it online here http://mhari.heliohost.org/t.php Any solution please
wolstech Posted March 2, 2017 Posted March 2, 2017 This should be closer to correct, though I didn't test it and there may be syntax errors. Your issues were both the SQL syntax described above, and also your IF statement was wrong. mysqli::query() doesn't return a boolean TRUE when you run a SELECT, it returns a mysqli_result object instead. Your IF was explicitly looking for a Boolean true, but since the function doesn't return true when a SELECT succeeds, the else section always runs (there's no database error, hence why $db->error was empty). The quick way to check if a mysqli::query() was successful is to check if the returned value is empty(). Boolean TRUE (the result of an INSERT, UPDATE, etc.), mysqli_result objects (the result of SELECT), etc. are considered to be non-empty. Boolean false from a failed query is always considered empty. <?php $db = new mysqli("localhost", "root", "", "default_db"); if ($db->connect_error) { die("Connection error"); } else { $sql = "SELECT * FROM users WHERE `nick` = '$u'"; $result = $db->query($sql); if (!empty($result)) //Check that it's not empty. Non-empty queries are successful. { $data = $result->fetch_array(MYSQLI_ASSOC); echo $data['email']; } else { echo "Query error: " . $db->error; } } ?>
Mhari Posted March 2, 2017 Author Posted March 2, 2017 Wow thank you!It's working now. Evidently because of wrong if statementThank you so much!
wolstech Posted March 2, 2017 Posted March 2, 2017 Glad to see you got it working Let us know if you need anything else.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now