调用for循环抛出异常的过程

时间:2013-12-30 10:38:15

标签: php stored-procedures

我在for循环中调用存储过程时遇到异常。我的代码是

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

$total      = 0;
$error      = 0; 
$trimmed = explode("\n",file_get_contents($fileName));

for ($i = 0; $i < 2; $i++) {    
    $qry_ins = "call sampleProcedureTest();";
     try {
         mysql_query($qry_ins) or throw_ex(mysql_error());
         $total++;
     } catch(Exception $e) {
         echo $e;
         echo  $qry_ins;
         echo "</br></br>";
         $error++;
     }
}


我搜索过但没有找到任何合理的理由和解决方案 注意:当我删除循环然后它工作正常。但我需要调用循环
请为此指导我

编辑#1:以下是程序

BEGIN
DECLARE uid VARCHAR(10) DEFAULT "0000";
SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 4) AS passwordStrin into uid FROM NO WHERE uid not in (Select ID from UIDLIST WHERE ID=uid);

    INSERT IGNORE INTO UIDLIST values(uid);

    select REPLACE(MSG,'URL',CONCAT('http://abc.org/?uid=',uid)) as message,SOURCE as sender,MODE as mode,uid  as uniqueid from testDB _MESSAGE;

    select REPLACE(MSG,'URL',CONCAT('http://abc.org/?uid=',uid)) as message,SOURCE as sender,MODE as mode,uid  as uniqueid from testDB_MESSAGE;
END

1 个答案:

答案 0 :(得分:0)

你需要从你的程序中删除select语句(如果它们对你没用)。问题是程序正在返回数据,这就是为什么当你从同一个程序调用它时它有问题循环中的脚本
也见Calling Stored Procedure in foreach loop - only first executed
另一个解决方案是(不推荐使用),以防您仍然希望将select statments in procedure放置为需要连接到db,调用过程,在每次迭代中释放连接。