我需要调用两个在MySQL中创建的过程,它们返回PHP文件中的记录。我不能一个接一个地调用程序。如果我调用一个又一个过程(从单个PHP页面),第二个过程将不会返回任何记录。
即使我执行一个在过程(返回记录)之后返回记录的select
雄蕊,也意味着select
语句不会返回任何值。为什么会发生这种情况?如何在一个PHP页面中一个接一个地调用两个过程?
嗨,我在这里添加我的代码。。。
model.php文件内容
<?php
class DBoperation {
function connect(){
$this->con=mysql_connect('localhost','root','');
mysql_select_db('test',$this->con);
}
function call proc2($empcode){
$this->sql="call proc1('$empcode',@strGrpId,@strTodate,@strAccPrTodt,@strRecalculate)";
}
function callproc2($strEmpCode){
$this->sql="call proc2('$strEmpCode',@a,@b,@c,@d,@e,@f)";
}
function execute(){
$res = mysql_query($this->sql);
return $res;
}
}
?>
view.php文件内容
<?php
require_once('../../model/model.php');
$empcode = $_SESSION['empcode'];
$obj=new DBoperation();
$obj->connect();
//calling first procedure
$obj->callproc1($empcode);
$res = $obj->execute();
while($result=mysql_fetch_array($res)) {
echo $result[0];
}
//calling second procedure
$obj->callproc2($empcode);
$fetch=$obj->execute();
while($fetch1=mysql_fetch_array($fetch)){
echo $fetch1[0];
}
?>
第二个程序没有任何回报。如果我在第二次procdeure之后执行任何select查询,我也不会返回任何内容。
试试这个
<?php
// New Connection
$db = new mysqli('localhost','user','pwd','Dbname');
// Check for errors
if(mysqli_connect_errno())
{
echo mysqli_connect_error();
}
// 1st Query
$result = $db->query("call proc1($empcode)");
if($result)
{
while ($row = $result->fetch_object())
{
echo $col1="$row->Column Name1"."<br>";
echo $col2="$row->Column Name2"."<br>";
}
// Free result set
$result->close();
$db->next_result();
}
// 2nd Query
$result = $db->query("call proc2('$empcode')");
if($result)
{
while ($row = $result->fetch_object())
{
echo $col1="$row->Column Name1"."<br>";
echo $col2="$row->Column Name2"."<br>";
}
// Free result set
$result->close();
$db->next_result();
}
else echo($db->error);
// Close connection
$db->close();
?>
执行第一次采购后放入$db->next_result();
。