Can someone please tell me why the first sql query works but the second thrown an error. I’ve not been able to find out why the integer value causes it to break.
Works:
$SQL = "SELECT * FROM Users WHERE userName = $uname AND pass = $pword";
Errors:
$SQL = "SELECT * FROM Users WHERE userName = $uname AND firstLogin = 1";
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 'AND firstLogin = 1' at line 1
Also if there’s some additional enlightenment you have to offer im all ears.
EDITED TO SHOW FUNCTIONAL CONNECTION Using PDO:
<?php
$uname = "userNameToSearchBy";
//PDO connection string
$user_name = "dbUsername";
$pass_word = "dbPass";
$database = "sdName";
$server = "dbServer";
$db = new PDO("mysql:host=".$server.";dbname=".$database.";", $user_name, $pass_word);
$stmt = $db->prepare("SELECT * FROM Users WHERE userName=? AND firstLogin=?");
$stmt->execute(array($uname, 1));
//Returns the rows from thge database that match the query
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
//Shows the number of rows returned
$message = $stmt->rowCount();
?>
<body>
<?php echo $message ?>
</body>
Outputs number of rows that match the sql criteria
Drop your
quote_smart()function, the deprecated mysql_ functions, and use PDO prepared statements instead. That should resolve your problem while improving the security and quality of your codebase.There’s a standard comment that gives more detail on this, check out the PDO tutorial at the end in particular: