1

如果在数据库中未找到与变量 $check_email 对应的电子邮件地址,如何从以下查询中返回错误。就目前而言,我可以将任何 gobbledygook 放入我网站上的文本字段中,它仍然会检索$result. (已经逃脱了字符串。及时进入准备好的陈述,所以请耐心等待)。

我希望有您在此处看到的错误消息。

$query  = "SELECT lastimage FROM scorers WHERE email = '{$check_email}'";
$result = mysqli_query($con, $query);

// Test if there was a query error
if (!$result) 
{
    die("We cannot find you in the database, please start again!");
}
while($row = mysqli_fetch_row($result)) 
{
    // output data from each row
    $image_number= $row[0];
}
4

2 回答 2

0
$sql = "select count(*) from scorers where email = '$email'";
$result = mysql_query($sql);
if($result > 0)
{
    die("We cannot find you in the database, please start again!");
}
于 2013-09-07T16:58:22.517 回答
0

$query  = "SELECT lastimage FROM scorers WHERE email = '{$check_email}'";
$sql = mysqli_query($con, $query);

// put the results into an array
$result = mysqli_fetch_assoc($sql);

// see if there was a result
if(count($result) > 0) {
    echo 'found';
} else {
    // don't use die(); handle your errors with an error handling function if you can
    $error_message = "We cannot find you in the database, please start again!";
}

编辑; 如果您不打算选择其他数据,Ankit Agrawal 的答案是一个更好的选择。

于 2013-09-07T16:56:37.673 回答