PHP mysqli查询以检查是否存在行 [英] PHP mysqli query to check if a row exist

查看:365
本文介绍了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);

现在您应该可以完成它

这篇关于PHP mysqli查询以检查是否存在行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆