Calling MYSQL Store Procedure using PHP PDO

Calling MYSQL Stored Procedure with PHP PDO.

1. Create your Store Procedure

PDO - SQL

 

2. Call your Store Procedure using PDO

PDO - use


 Source code below:

1. STORE PROCEDURE

CREATE DEFINER=`root`@`localhost` PROCEDURE `spu_user_create`(in Firstname varchar(50),
in Lastname varchar(50),
in Email varchar(50),
in Password varchar(50))
BEGIN
INSERT INTO user
(
user.Firstname,
user.Lastname,
user.Email,
user.Password)
VALUES
(Firstname,
Lastname,
Email,
Password);
END


2. PDO

public function Create($firstname,$lastname,$email,$password){

try{

$db=pdoconnection::connect();

$stmt = $db->prepare(“CALL spu_user_create(?,?,?,?)”);
$stmt ->bindValue(1, $firstname, PDO::PARAM_STR);
$stmt ->bindValue(2, $lastname, PDO::PARAM_STR);
$stmt ->bindValue(3, $email, PDO::PARAM_STR);
$stmt ->bindValue(4, $password, PDO::PARAM_STR);

$stmt -> execute();
$db=null;

return true;

}catch(PDOException $ex){
$db->rollBack();
echo “Error encountered in user registration.”.$ex->getMessage();
}
}

hope this helps.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s