验证成功执行mysql的最佳方法是什么,然后在您不能使用以下代码时返回结果:
$db = dbConnect();
//begin prepared statements to search db
$stmt = $db->prepare("SELECT email,authentication,email_confirm,externalid,password,id, admin FROM users WHERE email=?");
$stmt->bind_param('s',$email);
$stmt->execute();
$result = $stmt->get_result();
if (!$result){
//error statement
} if (!(mysqli_num_rows($result)==0)){
//action to perform
} else {
// no result returned
}
我在我的脚本中多次使用get_result,而我的托管服务提供商没有mysqlnd驱动程序,所以我必须重写很多代码。我知道我只能使用bind_result和fetch(),但是我需要一些帮助来重写代码,因为我的思维方式一直困在我第一次做的时候。
我也在使用mysqli而不是PDO。
答案 0 :(得分:1)
Mysqli fetch()
函数将返回3个值中的一个:
这意味着您可以像这样设置查询:
$db = dbConnect();
$query = "SELECT email,authentication,email_confirm,externalid,password,id, admin FROM users WHERE email=?";
$stmt = $db->prepare();
$stmt->bind_param('s',$email);
$stmt->execute();
$stmt->bind_result($email,$auth,$email_confirm,$externalid,$password,$id,$admin);
// Will only execute loop if returns true
$record_count = 0;
while($result = $stmt->fetch())
{
// Increment counter
$record_count++;
// Do something with bound result variables
echo("Email is $email");
}
// After the loop we either ran out of results or had an error
if($result === FALSE)
{
echo("An error occurred: " . $db->error());
}
elseif($record_count == 0)
{
echo("No records exist.");
}