Laravel 5 - 找不到 MySQL 存储过程

Posted

技术标签:

【中文标题】Laravel 5 - 找不到 MySQL 存储过程【英文标题】:Laravel 5 - Cannot find MySQL Stored Procedure 【发布时间】:2016-05-08 14:00:24 【问题描述】:

问题: 将我的 L4 代码转换为 L5.2 并收到以下错误:SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION cr_debug.ENTER_MODULE2 不存在(SQL:调用POPULATE_DAYS_TABLE(20, "01/29/2016")) & SQLSTATE[42000]: 语法错误或访问冲突: 1305 FUNCTION cr_debug.ENTER_MODULE2 不存在

更新:我不知道函数 FUNCTION cr_debug.ENTER_MODULE2 是什么。我不知道那是什么。这似乎是导致问题的原因,因为该函数在 mysql 中不存在,我也不知道它来自哪里。在我的 Laravel 5.2 安装中使用 Agent Ransack 我只找到提到 ENTER_MODULE2 的日志文件,并在我的 Laravel 4 安装中为 ENTER_MODULE2 进行文本搜索我只找到了一个提到该文本的 SQL 转储。下面列出了该转储。 我确实在我的存储过程中找到了对 ENTER_MODULE2 的引用。检查出来,稍后再发回。

SQL 转储:

DECLARE cr_stack_depth INTEGER DEFAULT cr_debug.ENTER_MODULE2('FIRST_DAY', 'contracts', 8, 100430)/*[cr_debug.1]*/;

  CALL cr_debug.UPDATE_WATCH3('`firstday`', `firstday`, 'DATE', cr_stack_depth)/*[cr_debug.2]*/;

CALL cr_debug.TRACE(4, 4, 0, 5, cr_stack_depth)/*[cr_debug.2]*/;

CALL cr_debug.TRACE(5, 5, 2, 67, cr_stack_depth)/*[cr_debug.2]*/;

SET @devart_debug_ret = ADDDATE(LAST_DAY(SUBDATE(firstday, INTERVAL 1 MONTH)), 1)/*[cr_debug.2]*/;

CALL cr_debug.LEAVE_MODULE(cr_stack_depth - 1)/*[cr_debug.2]*/;

RETURN @devart_debug_ret;

CALL cr_debug.TRACE(6, 6, 0, 3, cr_stack_depth)/*[cr_debug.2]*/;

CALL cr_debug.LEAVE_MODULE(cr_stack_depth - 1)/*[cr_debug.2]*/;

END ;;
DELIMITER ;

尝试:我搜索了 *** 和 Google 以查找正在发生的事情。我试图更改对 EXEC 的调用,并创建了另一个错误,指出我的 MySQL 版本不支持调用。两周前我刚刚安装了最新版本的 MySQL。我试图命名过程调用无济于事。我已经从存储过程中的函数调用中删除了所有空格(例如:IF (var) 现在是 IF(var) 或函数 (var) 现在是 function(var)。我已经删除了存储过程并收到了以下不同的错误: SQLSTATE[42000]:语法错误或访问冲突:1305 PROCEDUREcontracts.POPULATE_DAYS_TABLE 不存在(SQL:调用 POPULATE_DAYS_TABLE(29, "01/29/2016"))

请求:请协助找出问题所在,并在可能的情况下通过示例详细说明我做错了什么。

注意:为简洁起见,我已经严重缩短了 ContractController.php 文件。如果您需要查看存储过程,我也可以显示它们。存储过程在 MySQL 中。这适用于 Laravel 4。

调试信息: 错误 1 ​​个,共 2 个 Connection.php 第 390 行中的 PDOException: SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION cr_debug.ENTER_MODULE2 不存在

in Connection.php line 390
at PDOStatement->execute(array()) in Connection.php line 390
at Connection->Illuminate\Database\closure(object(MySqlConnection), 'call POPULATE_DAYS_TABLE(20, "01/29/2016")', array()) in Connection.php line 644
at Connection->runQueryCallback('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 611
at Connection->run('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 391
at Connection->statement('call POPULATE_DAYS_TABLE(20, "01/29/2016")')
at call_user_func_array(array(object(MySqlConnection), 'statement'), array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in DatabaseManager.php line 317
at DatabaseManager->__call('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in Facade.php line 218
at Facade::__callStatic('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in computer.php line 15
at computer::storedProcedureCall('20', '01/29/2016') in ContractController.php line 56
at ContractController->store()
at call_user_func_array(array(object(ContractController), 'store'), array()) in Controller.php line 76
at Controller->callAction('store', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ContractController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ContractController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ContractController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

错误 2 OF 2 Connection.php 第 651 行中的 QueryException: SQLSTATE[42000]: 语法错误或访问冲突: 1305 FUNCTION cr_debug.ENTER_MODULE2 不存在 (SQL: call POPULATE_DAYS_TABLE(20, "01/29/2016"))

in Connection.php line 651
at Connection->runQueryCallback('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 611
at Connection->run('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 391
at Connection->statement('call POPULATE_DAYS_TABLE(20, "01/29/2016")')
at call_user_func_array(array(object(MySqlConnection), 'statement'), array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in DatabaseManager.php line 317
at DatabaseManager->__call('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in Facade.php line 218
at Facade::__callStatic('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in computer.php line 15
at computer::storedProcedureCall('20', '01/29/2016') in ContractController.php line 56
at ContractController->store()
at call_user_func_array(array(object(ContractController), 'store'), array()) in Controller.php line 76
at Controller->callAction('store', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ContractController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ContractController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ContractController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

ContractController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Library\customer;
use App\Library\computer;

class ContractController extends Controller

    Protected $layout = 'master';
    public function store()
    
        // Receive input from Form
        $input = \Input::only([
            'contractterm_id', 'businesstype_id', 'company', 'bcity', 'bphone', 'bstate', 'bstraddr',
            'bzip', 'firstname', 'lastname', 'mobile', 'hcity',
            'hphone', 'hstate', 'hstraddr', 'hzip'
        ]);

        $morepcs_array = \Input::only('addtpcmake','addtpcmodel','addtpcserial','addtpcname');
    /*  
        // Debugging Code
        foreach ($morepcs_array as $textbox_name => $textbox)
            
                echo "<br><br>".$textbox_name;
                echo "<br>textbox quantity: ".sizeof($textbox);
                foreach($textbox as $value) 
                    if ($value == NULL) 
                        echo "<br>NULL";
                    
                    else 
                        echo "<br>".$value;
                    
                
            
    */  
        $customer = new customer($input); // Create new customer object. Store $input into this object.
        $computer = new computer; // Create new computer object
        $computer->addtpcs = \Input::get('addtpcs'); // Get the form data for addtpcs and prepare to store it in a database table named addtpcs.
        $computer->save(); // Save the information into the database table addtpcs from the computer object.
        $customer->computer()->associate($computer); // Using the associate function, store the id from the computer table in the database to the customer_id table in the database.
        $customer->save(); // send all of the data to the customer table in the database.

        // $startdate = new day;
        $startdate = \Input::get('contract_date');
        // $customer->startdate()->save($startdate);

        //Log::info('$startdate from controller before it is passed to the StoredProcedureCall method: ' . $startdate);
        //Log::info('$customer->id from controller before it is passed to the StoredProcedureCall method: ' . $customer->id);
        //Computer::storedProcedureCall($customer->id,$startdate);
        //$days = Day::all()->last(); // Never do this.  For testing purposes only.
        // Figure out total contract cost based upon Contract Term and Business Type
        computer::storedProcedureCall($customer->id,$startdate); // Call the MySQL stored procedure.

computer.php:

Class computer extends \Eloquent 
 protected $guarded = array();

 public function customer() 
    return $this->hasMany('App\Library\customer');
 

 public static function storedProcedureCall($customer, $contract_date) 
         //Log::info('$contract_date after it is received from the controller: ' . $contract_date);
         //Log::info('$data after it is received from the controller: ' . $data);
         return \DB::statement('call POPULATE_DAYS_TABLE(' . $customer . ', "'.$contract_date.'")');
    
 public static $rules = array();
  

错误日志:

[2016-01-31 16:36:20] local.ERROR: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION cr_debug.ENTER_MODULE2 does not exist in C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php:390
Stack trace:
#0 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(390): PDOStatement->execute(Array)
#1 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(644): Illuminate\Database\Connection->Illuminate\Database\closure(Object(Illuminate\Database\MySqlConnection), 'call POPULATE_D...', Array)
#2 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(611): Illuminate\Database\Connection->runQueryCallback('call POPULATE_D...', Array, Object(Closure))
#3 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(391): Illuminate\Database\Connection->run('call POPULATE_D...', Array, Object(Closure))
#4 [internal function]: Illuminate\Database\Connection->statement('call POPULATE_D...')

更多错误 - 相同的日志文件:

Next Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION cr_debug.ENTER_MODULE2 does not exist (SQL: call POPULATE_DAYS_TABLE(23, "01/29/2016")) in C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php:651
Stack trace:
#0 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(611): Illuminate\Database\Connection->runQueryCallback('call POPULATE_D...', Array, Object(Closure))
#1 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(391): Illuminate\Database\Connection->run('call POPULATE_D...', Array, Object(Closure))
#2 [internal function]: Illuminate\Database\Connection->statement('call POPULATE_D...')
#3 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(317): call_user_func_array(Array, Array)

【问题讨论】:

你能记录下 laravel 发送的 sql 并分享吗?我无法从代码中的错误中看到函数名称 报错信息中的函数是否存在?如果是,你使用的mysql用户是否有执行该功能的权限? @Shadow - 存储过程确实存在。用户确实有权执行该功能。 @MikeMiller - 我会记录它,但它在这一行: t Connection->Illuminate\Database\closure(object(MySqlConnection), 'call POPULATE_DAYS_TABLE(20, "01/29/ Connection.php 第 644 行中的 2016")', array()) - POPULATE_DAYS_TABLE 是存储过程的名称。 @MikeMiller - 我不确定日志文件会有多大帮助,但是我已经附上了两个错误的日志文件。如果不是通过 laravel.log 并使用类似的东西,我不确定如何从 Laravel 获取确切的 SQL 查询: \Log::info(\DB::statement('call POPULATE_DAYS_TABLE(' . $customer . ', "' .$contract_date.'")')); 【参考方案1】:

真的很奇怪,但是不知何故,MySQL 创建了一堆函数,这些函数被添加到我的存储过程中,导致它崩溃。

2016 年 2 月 1 日更新:Shadow 指出额外的调试来自 dbforge studio,我确实在几年前下载了它来调试存储过程。答案仍然是一样的。如果遇到这个问题,要么从备份中恢复,要么删除调试垃圾,要么重新安装调试软件。

这方面的一个例子是存储过程应该看起来像:

真实存储过程

BEGIN
SET contract_date = STR_TO_DATE(contract_date, '%m/%d/%Y'); /* Changes parameter sent it to an actual date */
SELECT DATE_FORMAT(contract_date, '%Y-%m-%d') INTO @start_date; /* Store proper date format for Contract End Dates */

修改的 MYSQL 存储过程

BEGIN
DECLARE cr_stack_depth INTEGER DEFAULT cr_debug.ENTER_MODULE2('POPULATE_DAYS_TABLE', 'contracts', 7, 100430)/*[cr_debug.1]*/;
CALL cr_debug.UPDATE_WATCH3('`customer_id`', `customer_id`, 'INT', cr_stack_depth)/*[cr_debug.2]*/;
CALL cr_debug.UPDATE_WATCH3('`contract_date`', `contract_date`, 'TEXT', cr_stack_depth)/*[cr_debug.2]*/;
CALL cr_debug.TRACE(4, 4, 0, 5, cr_stack_depth)/*[cr_debug.2]*/;
CALL cr_debug.TRACE(5, 5, 0, 59, cr_stack_depth)/*[cr_debug.2]*/;
SET contract_date = STR_TO_DATE(contract_date, '%m/%d/%Y');
CALL cr_debug.UPDATE_WATCH3('contract_date', contract_date, '', cr_stack_depth)/*[cr_debug.1]*/; /* Changes parameter sent it to an actual date */
CALL cr_debug.TRACE(6, 6, 0, 63, cr_stack_depth)/*[cr_debug.2]*/;
SELECT DATE_FORMAT(contract_date, '%Y-%m-%d') INTO @start_date;

解决方法是删除 MySQL 中的存储过程,然后从我拥有的备份中重新导入它们。如果您没有备份,则必须手动删除添加到其中的所有垃圾。

【讨论】:

好东西,很高兴你能成功。不确定我的解决方案是否会起作用,如果它是 mysql 附加了所有废话,但无论哪种方式都是好东西 @MikeMiller - 好的,我会编辑这个并将我的标记为答案。再次感谢您在此方面为我提供帮助。它为我指明了正确的方向。 你没有安装 dbforge studio 或 sg 类似的服务器端调试东西吗? @Shadow - 你是绝对正确的......该死的......完全忘记了这一点。很久以前我确实安装了dbforge studio,它是为了调试我的存储过程......我当然没有在新服务器上安装它。啊!! - 谜团解决了额外调试的来源。答案还是一样... 把存储过程中的调试资料拿出来让它正常工作或者重新安装调试软件。【参考方案2】:

由于对存储过程的调用很奇怪,您可以尝试将其传递给底层 PDO 对象,例如..

$db = \DB::connection()->getPdo();
$stmt=$db->prepare($sql);
$stmt->bindParams($params);
$stmt->execute();

就像您在原生 PHP 中所做的那样。为简洁起见已编辑,但我希望你能明白

【讨论】:

以上是关于Laravel 5 - 找不到 MySQL 存储过程的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5 自定义验证 - 自定义 MySQL 查询

Docker Laravel Mysql:找不到驱动程序

Laravel 外键约束格式不正确我已经搜索过,找不到答案

Laravel 5.1 找不到路由对象

Ubuntu 14.04.2 Laravel 4.2.0 PDOException 找不到驱动程序 MySQL

找不到错误类“HTML”-Laravel 5.4