什么是PDO执行语句的替代mysqli执行语句?

时间:2019-04-27 13:50:14

标签: php arrays mysqli pdo execute

我有一小段pdo语句,我想将其用作mysqli语句,但是我似乎并没有得到它的帮助。

我正在尝试将mysqli-> execute(array())从pdo语句转换为mysqli语句。

据我了解,有execute()语句,该语句不希望mysqli使用任何参数,但使用PDO则需要一个参数?

这是代码,我想将其转换为mysqli语句:

$sql = "SELECT * FROM users WHERE username = :username";
        $sth = $GLOBALS["DB"]->prepare($sql);
        $sth->execute(array(":username" => $username));
        $result = $sth->fetchAll();

        if (!empty($result)) {
            $privUser = new PrivilegedUser();
            $privUser->user_id = $result[0]["user_id"];
            $privUser->username = $username;
            $privUser->password = $result[0]["password"];
            $privUser->email_addr = $result[0]["email_addr"];
            $privUser->initRoles();
            return $privUser;
        } else {
            return false;
        }

这是我当前的代码:

$sql = "SELECT * FROM users WHERE user_id = ?";
          $stmt = $mysqli -> prepare($sql);
          $stmt->bind_param('s', $user_id);
// line below is what i need to convert to a mysqli statement
          $stmt->execute(array(":user_id" => $user_id));
          //$stmt->store_result();
          $result = $stmt->fetchAll();

          if (!empty($result)) {
              $privUser = new PrivilegedUser();
              $privUser->user_id = $result[0]["user_id"];
              $privUser->username = $user_id;
              $privUser->password = $result[0]["password"];
              $privUser->email_addr = $result[0]["email_addr"];
              $privUser->initRoles();
              return $privUser;
          } else {
              return false;
          }

1 个答案:

答案 0 :(得分:0)

像上面的Nigel Ren所说,Quote: “对于mysqli,对bind_param()的调用是链接绑定变量的唯一方法,因此对execute()的调用实际上就是$ stmt-> execute();”