您不能混合使用查询语句与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')? –