Wednesday, April 08, 2009

How to call MySQL stored procedure in PHP with output parameter?

Check the below example.

//Say following is the store procedure you have

DELIMITER //
DROP PROCEDURE IF EXISTS yourdbname.gsp_GetCountryList //
CREATE PROCEDURE yourdbname.gsp_GetCountryList()
BEGIN
SELECT Id,strCountryName from youtable;
END//

//the above has to be run on mysql prompt


//change the php variables according to your setting


function iconnect()
{
$mysqli = new mysqli($YOURHOST, $YOURUSERNAME, $YOURPASSWORD, $YOURDATABASE);

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli;
}

$retval=iconnect();

$sql1 = "call sp1()";
$query1 = $retval->query($sql1);
while ($data1 = $query1->fetch_row()) {
echo "
\n".$data1[0].":".$data1[1];
}

No comments: