简体   繁体   中英

How can i run a check in PHP to see if my MYSQL Stored Procedure ran correctly?

I'm used to working with MSSQL but recently have had to use MYSQL, I know it is similar but the way things are done are confusing me slightly.

My question is after I have called a stored procedure (insert, update, select, delete) how can I do a check within PHP that the call was successful before returning data from my function?

CURRENT MYSQL (SELECT) call from PHP:

public function login($email){
        $stmt = $this->connection->prepare('call loginUser(?)');
        $stmt->bind_param("s", $email);

        $stmt->execute();

        // if($stmt === false){
        //  return false;           HOW I CURRENTLY DO IT IN MSSQL
        // }
        $result = $stmt->get_result();
        $row = mysqli_fetch_assoc($result);

        $stmt->close();
        return json_encode(array("success"=>true));
    }

CURRENT MYSQL (INSERT) call from PHP:

public function registerUser($params){
        // Prepare and Bind
        $stmt = $this->connection->prepare('call addUser(?,?,?,?,?)');
        $stmt->bind_param("sssss", $firstname, $surname, $displayname, $email, $password);

        $firstname = $params['firstname'];
        $surname = $params['surname'];
        $displayname = $params['firstname'] . ' ' . $params['surname'];
        $email = $params['email'];
        $password = password_hash($params['password'], PASSWORD_DEFAULT);

        $stmt->execute();
        $stmt->close();

        return json_encode(array("success"=>true));
    }

try this

 public function registerUser($params= null){
    // Prepare and Bind
    if ($param==0) {
      echo '<script>alert(\'saved\')</script>';
      $stmt = $this->connection->prepare('call addUser(?,?,?,?,?)');
      $stmt->bind_param("sssss", $firstname, $surname, $displayname, $email, $password);

      $firstname = $params['firstname'];
      $surname = $params['surname'];
      $displayname = $params['firstname'] . ' ' . $params['surname'];
      $email = $params['email'];
      $password = password_hash($params['password'], PASSWORD_DEFAULT);

      $stmt->execute();
      $stmt->close();
    }  

  }

执行返回布尔值,可以这样检查

if (! $stmt->execute()) { return false; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM