在PHP中进行多个(查询)检查(优化)

时间:2014-06-02 08:59:24

标签: php mysqli mysqli-multi-query

我想问,是否有任何如何缩短我的剧本

首先检查,如果获得uid,请执行第二次和第三次检查

$result = mysqli_fetch_row(mysqli_query($con, "SELECT MAX(`uid`) FROM `pvpgn_bnet`"));
if($result) {
    Do Second and First Check();
}

第二次检查

$result = mysqli_fetch_row(mysqli_query($con, "SELECT * FROM `pvpgn_bnet` WHERE acct_email = '" . $email . "'"));
    if($result) {
        errorMsg();
    }

第三次检查

$result = mysqli_fetch_array(mysqli_query($con, "SELECT `uid` FROM `pvpgn_bnet` WHERE acct_username = '" . $username . "'"), MYSQLI_BOTH);
if($result) {
    errorMsg();
}

1 个答案:

答案 0 :(得分:1)

我不知道第一个过滤器是什么(它没有意义),但第二个和第三个可以组合。考虑这个例子:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$stmt = $mysqli->prepare("SELECT * FROM pvpgn_bnet WHERE acct_email=? OR acct_username=?")
$stmt->bind_param("ss", $email, $username);
$stmt->execute();
// other processes

$res = $stmt->get_result();
while($row = $res->fetch_array(MYSQLI_ASSOC)) {
    // what ever you want to do here
}