您不能混合使用查詢語句與PHP語句。提取期望的結果和chec的查詢k如果該查詢中有任何行,則爲k。
我會告訴你一個例子:
$query = "SELECT * FROM `TABLE_NAME` WHERE `field` = '1' && `email`='$email'"; //Create similar query
$result = mysqli_query($query, $link); //Query the server
if(mysqli_num_rows($result)) { //Check if there are rows
$authenticated = true; //if there is, set a boolean variable to denote the authentication
}
//Then do what you want
if($authenticated) {
echo "Edit Server";
} else {
echo "Add Server";
}
由於阿龍已經顯示出這樣的努力,以鼓勵安全的代碼在我的例子。這是你如何安全地做到這一點。 PDO庫提供了以安全方式將參數綁定到查詢語句的選項。所以,這裏是如何做到這一點。
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); //Create the connection
//Create the Query Statemetn
$sth = $dbh->prepare('SELECT * FROM `TABLE_NAME` WHERE field = :field AND email = :email');
//Binds Parameters in the safe way
$sth -> bindParam(':field', 1, PDO::PARAM_INT);
$sth -> bindParam(':email', $email, PDO::PARAM_STRING);
//Then Execute the statement
$sth->execute();
$result = $sth->fetchAll(); //This returns the result set as an associative array
'WHERE'是SQL而不是PHP語法。如果你可以顯示更多關於'$ row'的信息(存儲在它裏面;它是如何填充的),可能有一種方法可以在PHP命名法中執行任務(如果不在SQL查詢本身內)。 –
好吧,添加了更多關於行的信息,但它幾乎只是提取$ SQL。 – user1907276
'row'怎麼可能是'1' _and_'$ email'(除非'$ email = 1')? –