简体   繁体   English

在PHP中使用out参数执行存储过程

[英]Executing stored procedure with out parameter in PHP

This is my stored procedure: 这是我的存储过程:

DELIMITER $$
CREATE PROCEDURE `insert_request_info`(in request_type text, in adequeate bool, in status bool, in email bool, in pickup bool, in comments text, 
    in request_date timestamp, in pickup_date timestamp, in email_date timestamp, in psid varchar(40), in dso_id varchar(40), out last_id real)
BEGIN

insert into request_info values(DEFAULT, request_type, adequeate, status, email, pickup, comments, request_date, pickup_date, email_date, psid, dso_id);
SET last_id = LAST_INSERT_ID();
END

I am trying to execute this stored procedure in the following way: 我试图以以下方式执行此存储过程:

$request_info_query = "CALL insert_request_info('invitation', false, false, false, false, '', Now(), timestamp(0), timestamp(0), $student_psid, null, @out);SELECT @out;";
$result = mysql_query($request_info_query, $con);

When i execute the above code it gives me the following error: 当我执行以上代码时,它给了我以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT @out' at line 1CALL insert_request_info('invitation', false, false, false, false, '', Now(), timestamp(0), timestamp(0), 12, null, @out);SELECT @out; 检查与您的MySQL服务器版本对应的手册以获取正确的语法以在第1行的'SELECT @out'附近使用CALL insert_request_info('invitation',false,false,false,false,'',Now(),timestamp(0) ,timestamp(0),12,null,@out); SELECT @out;

I had checked other solutions, many had done the same. 我检查了其他解决方案,很多都这样做。 But I still get errors, any help? 但是我仍然会出错,有什么帮助吗?

In PHP you can only execute a single statement at once. 在PHP中,您一次只能执行一条语句。 Use two different calls to execute this code. 使用两个不同的调用来执行此代码。 eg: 例如:

// Insert info
$insert_query = "CALL insert_request_info('invitation', false, false, false, false, '', Now(), timestamp(0), timestamp(0), $student_psid, null, @out);";
$insert_result = mysql_query($insert_query, $con);

// Get query result
$request_info_query = "SELECT @out;";
$result = mysql_query($request_info_query, $con);

mysql_query will only send one query at at time , and the mysql extension is deprecated; mysql_query只会发送一个查询 ,并且不建议使用mysql扩展; you should be using the improved extension, mysqli , and more specifically the function mysqli_multi_query . 您应该使用改进的扩展名mysqli ,更具体地说是mysqli_multi_query函数。

That being said, I believe sending multiple queries to mysql_query simply ignores the queries after the first, so you shouldn't be seeing that error from MySQL. 话虽这么说,我相信将多个查询发送到mysql_query只会在第一个查询之后忽略该查询,因此您不应从MySQL看到该错误。 It looks like semicolons in mysql_query cause this error. 看起来mysql_query分号会导致此错误。 You should also consider explicitly changing the delimiter back after defining your procedure, to avoid the potential for future errors. 您还应该考虑在定义过程之后显式改回分隔符,以避免潜在的将来发生错误。

DELIMITER $$
CREATE PROCEDURE ...

END$$
DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM