【问题标题】:Running a mysql stored procedure using php使用 php 运行 mysql 存储过程
【发布时间】:2016-08-17 18:17:03
【问题描述】:

你好,我正在尝试运行一个名为“sp_tbl_prototype_edit”的mysql存储过程

这个存储过程有两个参数:

  • p_Techname (VARCHAR(10))

  • p_Value (INT)

看起来下面的 PHP 函数没有按我的意愿保存(编辑数据库中的值)值。

function edit_record($title, $value){

    // Prepares IN parameters
    $mysqli->query("SET p_Techname = '" . $title . "'");
    $mysqli->query("SET p_Value = '" . $value ."'");

    // Call stored procedure
    if(!$mysqli->query("CALL sp_tbl_prototype_edit (@p_Techname, @p_Value)"))
    {
    if($mysqli) $mysqli->close(); // Close DB connection
    //header('Location: error.php?error=QueryFailure');
    die();
    }

    if($mysqli) $mysqli->close(); // Close DB connection
}

请问您有什么解决此问题的建议吗?提前非常感谢。

【问题讨论】:

  • 如果查询失败,您只需重定向。您应该让系统告诉您失败的原因,例如die($mysqli->error);。请注意,您很容易受到 sql injection attacks 的攻击
  • 为什么不直接传参数$mysqli->query("CALL sp_tbl_prototype_edit ($title, $value)")?我认为您的临时变量不会在查询之间持续存在。如果可行,那么您应该参数化它们 - 尤其是如果 $input$value 是用户提交的数据。

标签: php mysql stored-procedures


【解决方案1】:

你有两个选择:

  1. @为前缀正确设置会话变量:

    $mysqli->query("SET @p_Techname = '" . $title . "'");
    $mysqli->query("SET @p_Value = '" . $value ."'");
    
  2. 使用值而不是变量调用存储过程:

    $mysqli->query("CALL sp_tbl_prototype_edit ('" . $title . "', '" . $value ."')")
    

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 2011-02-10
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    相关资源
    最近更新 更多