Search code examples
phparraysmysqlipdoexecute

What is the alternative mysqli execute statement for a PDO execute statement?


I have a snippet of a pdo statement, which i want to use as mysqli statement, but i don't seem to get the hang of it somehow.

I'm trying to "convert" a mysqli->execute(array()) from a pdo statement into a mysqli statement.

From what i understood, there is the execute() statement, which expects no parameter with mysqli, but with PDO it expects a parameter?

This is the code, which i want to convert as a mysqli statement:

$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;
        }

and this is what my current code is:

$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;
          }

Solution

  • Like Nigel Ren above stated, Quote: "With mysqli, the call to bind_param() is the only way to link bind variables, so the call to execute() will literally be that $stmt->execute();"