Create and call procedure in PHP and MySql

A stored procedure has a header and a body using to declarative SQL statements stored inside the database catalog.
There are three diffrent ways to pass parameters in procedures :
1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters
Database table
Create user table with name and email.

CREATE TABLE user
(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(50) UNIQUE,
);

syntax to create stored procedure:-

DELIMITER //
CREATE PROCEDURE or replace UserInfo()

 BEGIN
   SELECT name,email FROM users;
 END 

Create config.php file

//connect to database
$conn=mysql_connect(“hostname”,”database username”,”database password”) or die(mysql_error());

//select your database name
$dbc=mysql_select_db(“testdb”,$conn);

if (!$dbc) {
    die(‘Could not select: ‘ . mysql_error());
}

?>

Call procedure in your php code:-

//include db config file
include(‘config.php’);

//call the procedure
$sql=mysql_query(‘call UserInfo()’);
if (!$sql) {
    die(‘syntax error in procedure: ‘ . mysql_error());
}

while($row=mysql_fetch_object($sql)):

echo $row->name.’
‘;
echo $row->email.’
‘;

endwhile;

?>

Insert stored procedure:-

DELIMITER //
CREATE PROCEDURE insert(IN name VARCHAR(50),IN email VARCHAR(50))

BEGIN

SET @username=username;
SET @name=name;

PREPARE STMT FROM
“INSERT INTO users(username,name) VALUES (?,?)”;

EXECUTE STMT USING @username,@name;

END

Insert.php page 

//include db config file
include(‘config.php’);

$name=’freewebmentor’;
$name=’freewebmentor@gmail.com’;
$sql=mysql_query(‘CALL insert(“$name”,”$email”)’);

if (!$sql) {
    die(‘Oops something wrong : ‘ . mysql_error());
}

?>

   Source:- Freewebmentor