php调用MySQL存储过程的方法集合(推荐)
程序员文章站
2023-11-10 15:18:16
类型一:调用带输入、输出类型参数的方法复制代码 代码如下:$returnvalue = '';try { mysql_query ( "set @return"...
类型一:调用带输入、输出类型参数的方法
$returnvalue = '';
try {
mysql_query ( "set @return" );
$spname = 'p__test_getinfo1';
mysql_query ( "call $spname(@return, '{$userid}', '{$pwd}')" ) or die ( "[$spname]query failed:" . mysql_error () );
$result_return = mysql_query ( "select @return" );
$row_return = mysql_fetch_row ( $result_return );
$returnvalue = $row_return [0];
} catch ( exception $e ) {
echo $e;
}
echo $returnvalue; //输出来自存储过程中输出的变量
类型二:调用带多个输出类型和多个输入类型参数的方法
$userid = 0;
try{
mysql_query("set @message");
mysql_query("set @id");
mysql_query("call p__test_login(@message, @id, '{$userid}', '{$pwd}')", $conn) or die("query failed:".mysql_error());
$result_mess = mysql_query("select @message");
$result_uid = mysql_query("select @id");
$row_mess = mysql_fetch_row($result_mess);
$row_uid = mysql_fetch_row($result_uid);
$proc_error = $row_mess[0];
$uid = $row_uid[0];
}
catch( exception $e )
{
echo $e;
}
echo 'proc return message:'$proc_error.'<br/>'; //输出来自存储过程中输出的变量
echo 'user id:'.$uid; //获取用户id
类型三:调用带返回结果集的方法
try {
$spname = 'p__test_getdata';
$query = mysql_query ( "call $spname()", $conn ) or die ( "[$spname]query failed:".mysql_error() );
while ( $row = mysql_fetch_array ( $query ) ) {
echo $row ['provinceid'].'::'.$row ['provincename']; //输出数据集
}
} catch ( exception $e ) {
echo $e;
}
类型四:调用带返回多个结果集的方法(目前只能通过mysqli来实现~~)
//php
$rows = array ();
$db = new mysqli($server,$user,$psd,$dbname);
if (mysqli_connect_errno()){
$this->message('can not connect to mysql server');
}
$db->query("set names utf8");
$db->query("set @message");
if($db->real_query("call p__test_getdata2(@message)")){
do{
if($result = $db->store_result()){
while ($row = $result->fetch_assoc()){
array_push($rows, $row);
}
$result->close();
}
}while($db->next_result());
}
$db->close();
print_r($rows);
//procedure
……
select * from t1 where ……
select * from t2 where ……
……
复制代码 代码如下:
$returnvalue = '';
try {
mysql_query ( "set @return" );
$spname = 'p__test_getinfo1';
mysql_query ( "call $spname(@return, '{$userid}', '{$pwd}')" ) or die ( "[$spname]query failed:" . mysql_error () );
$result_return = mysql_query ( "select @return" );
$row_return = mysql_fetch_row ( $result_return );
$returnvalue = $row_return [0];
} catch ( exception $e ) {
echo $e;
}
echo $returnvalue; //输出来自存储过程中输出的变量
类型二:调用带多个输出类型和多个输入类型参数的方法
复制代码 代码如下:
$userid = 0;
try{
mysql_query("set @message");
mysql_query("set @id");
mysql_query("call p__test_login(@message, @id, '{$userid}', '{$pwd}')", $conn) or die("query failed:".mysql_error());
$result_mess = mysql_query("select @message");
$result_uid = mysql_query("select @id");
$row_mess = mysql_fetch_row($result_mess);
$row_uid = mysql_fetch_row($result_uid);
$proc_error = $row_mess[0];
$uid = $row_uid[0];
}
catch( exception $e )
{
echo $e;
}
echo 'proc return message:'$proc_error.'<br/>'; //输出来自存储过程中输出的变量
echo 'user id:'.$uid; //获取用户id
类型三:调用带返回结果集的方法
复制代码 代码如下:
try {
$spname = 'p__test_getdata';
$query = mysql_query ( "call $spname()", $conn ) or die ( "[$spname]query failed:".mysql_error() );
while ( $row = mysql_fetch_array ( $query ) ) {
echo $row ['provinceid'].'::'.$row ['provincename']; //输出数据集
}
} catch ( exception $e ) {
echo $e;
}
类型四:调用带返回多个结果集的方法(目前只能通过mysqli来实现~~)
复制代码 代码如下:
//php
$rows = array ();
$db = new mysqli($server,$user,$psd,$dbname);
if (mysqli_connect_errno()){
$this->message('can not connect to mysql server');
}
$db->query("set names utf8");
$db->query("set @message");
if($db->real_query("call p__test_getdata2(@message)")){
do{
if($result = $db->store_result()){
while ($row = $result->fetch_assoc()){
array_push($rows, $row);
}
$result->close();
}
}while($db->next_result());
}
$db->close();
print_r($rows);
//procedure
……
select * from t1 where ……
select * from t2 where ……
……