Wednesday, 21 November 2012

Create Stored Procedure With Php Mysql

Here we will discus, how to use procedure with mysql and php.


Procedure is so much beneficial for  long join query, it will save the time when you load your page.procedures are same as functions .

=>firstly we Create  simple stored procedure into our database

create PROCEDURE testProc()
begin
select field1 from table;
end ;


=>Now we can use the testProc procedure into our php code

<?php $rs=mysql_query('call testProc()'); ?>


Note:Remeber one thing that after using procedure your database connection will automatically closed.

=>Now we will create parameterize procedure into our database

CREATE PROCEDURE  get_users(IN var1 INT,OUT var2 VARCHAR(100),OUT var3 VARCHAR(100))
BEGIN
SELECT first_name, last_name
INTO var2, var3
FROM users
WHERE users_id = var1;
END


=>Now we can use the get_users procedure into our php code
<?php
$rs = mysql_query( ‘CALL get_user(1, @first, @last)’ );
$rs = mysql_query( ‘SELECT @first, @last’ );
while($row = mysql_fetch_assoc($rs))
{
var_dump($row);
}

?>


Note: Customize this code according to your requirement

No comments:

Post a Comment