简体   繁体   中英

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; 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;

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. 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; you should be using the improved extension, mysqli , and more specifically the function 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. It looks like semicolons in mysql_query cause this error. 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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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