简体   繁体   English

在 Laravel 中使用带有 OUT 参数的存储过程

[英]Using stored procedure with an OUT parameter in Laravel

I'm having a problem calling a stored procedure that has two OUT parameters.我在调用具有两个 OUT 参数的存储过程时遇到问题。 I cannot access them.我无法访问它们。

Here is my procedure's first line:这是我的程序的第一行:

PROCEDURE `validate_reservation`(IN people INT, IN r_date DATETIME,IN place_id INT,IN max_people INT,IN more_people TINYINT(1),OUT r_status CHAR(20),OUT message CHAR(100))

And here is how I call it from Laravel 5:这是我从 Laravel 5 调用它的方式:

DB::statement("call validate_reservation(4,'2016-04-26 20:30',1,10,1,$status,$message)");

I don't know if I have to pass two empty variables and they will turn the values of the output or if that is the return of the statement.我不知道是否必须传递两个空变量,它们会改变输出的值,或者这是否是语句的返回值。

If I pass two empty variables Laravel tells me they are not defined.如果我传递两个空变量 Laravel 告诉我它们没有定义。 If I don't pass them, Laravel tells me the procedure is waiting for 7 parameters instead of 5.如果我不传递它们,Laravel 会告诉我该过程正在等待 7 个参数而不是 5 个。

With OUT parameters, you're dealing with MySQL variables - these are prefixed with @.使用 OUT 参数,您正在处理 MySQL 变量 - 这些变量以 @ 为前缀。

So, use @status, @message instead of $status, $message .因此,使用@status, @message而不是$status, $message Also, you may want to use binding to pass the other values.此外,您可能希望使用绑定来传递其他值。

These won't populate PHP variables in any case.在任何情况下,这些都不会填充 PHP 变量。 If you want to get them in PHP, you'll need to SELECT them, eg SELECT @status, @message using DB::select .如果你想在 PHP 中得到它们,你需要选择它们,例如SELECT @status, @message使用DB::select

This way work for me:这种方式对我有用:

DB::select("call validate_reservation(4,'2016-04-26 20:30',1,10,1,@status,@message)");

$results = DB::select('select @status as status, @message as message ');

return dd($results[0]->message);

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

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