有人可以告诉我为什么第一个 sql 查询有效,但第二个抛出错误。我一直无法找出整数值导致它中断的原因。
作品:
$SQL = "SELECT * FROM Users WHERE userName = $uname AND pass = $pword";
错误:
$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
此外,如果有一些额外的启示,你必须全神贯注。
编辑使用 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>
输出符合 sql 条件的行数