PHP mysqli查询以检查是否存在行
问题描述:
我有一个mysql表,我想检查是否存在一行,其中columnA = $ a和$ columnB = $ b.我不需要从那里选择任何东西.什么是有效的查询呢?目前,我正在这样做,
I have a mysql table, I want to check if a row exists where columnA=$a and $columnB=$b. I dont need to select anything from there. What should be efficient query for that? Currently Im doing like,
if ($stmt = mysqli->prepare("SELECT * FROM TABLE WHERE columnA=? && columnB= ? LIMIT 1")) {
$stmt->bind_param("ss", $a, $b);
$stmt->execute();
$stmt->store_result();
$count=$stmt->num_rows;
$stmt->close();
}
return ($count > 0 ? true : false);
答
尝试一下:
if ($stmt = $mysqli->prepare("SELECT COUNT(*) FROM TABLE WHERE columnA=? && columnB=?")) {
$stmt->bind_param("ss", $a, $b);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$stmt->close();
}
return ($count > 0 ? true : false);
现在您应该可以完成它