Laravel 4 调用mysql存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16982381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:55:00  来源:igfitidea点击:

Laravel 4 call mysql stored procedure

phpstored-procedureslaravellaravel-4

提问by Dainius Dainius

im trying to migrate from Laravel 3 to Laravel 4

我正在尝试从 Laravel 3 迁移到 Laravel 4

In Laravel 3 I used to call procedure like this and everything worked fine.

在 Laravel 3 中,我曾经像这样调用过程,一切正常。

public function get_gen_dist($skip = 0, $take = 0) {
    $countries = Country::skip($skip)->take($take)->get();
    foreach ($countries as $country) {
        DB::query('CALL dist_proc(' . $country->id . ');');
    }
}

In Laravel 4 I changed "DB::query" to "DB::raw", no errors, and procedure is not called (at least procedure not executed), var_dump'ed result with "DB::raw" looks

在 Laravel 4 中,我将“DB::query”更改为“DB::raw”,没有错误,并且没有调用过程(至少没有执行过程),带有“DB::raw”的 var_dump'ed 结果看起来

object(Illuminate\Database\Query\Expression)[269]
protected 'value' => string 'CALL dist_proc(107)' (length=19)

Also tried:

还试过:

...    
foreach ($countries as $country) {
      $country_id = $country->id;
      $db = DB::connection()->getPdo();
      $stmt = $db->prepare("CALL dist_proc(?);");
      $stmt->bindValue(1, $country_id, PDO::PARAM_INT);
      $stmt->execute();
}

but on second loop call I get

但在第二个循环调用我得到

ErrorException
Packets out of order. Expected 1 received 5. Packet size=10

tried to "$stmt->closeCursor();" after "$stmt->execute();", but now success.

试图“$stmt->closeCursor();” 在“$stmt->execute();”之后,但现在成功了。

Without loop or if loop runs only once, procedure executes successully.

没有循环或如果循环只运行一次,过程成功执行。

How to call procedure in loop?

如何在循环中调用过程?

Thank you

谢谢

回答by Antonio Carlos Ribeiro

Try to run it this way:

尝试以这种方式运行它:

public function get_gen_dist($skip = 0, $take = 0) {
    $countries = Country::skip($skip)->take($take)->get();
    foreach ($countries as $country) {
        DB::statement(DB::raw('CALL dist_proc(' . $country->id . ');'));
    }
}