douwen5741 2016-11-14 23:44
浏览 73

使用MSSQL的PDO在写为预准备语句时导致零插入元素

I have been going over this query repeatedly trying to understand why it won't work as a prepared statement when run in PDO, but seems to work fine when set up as a stored procedure. Can anyone help provide some insight into where I may be running in to trouble?

Here is the relevant PHP:

try {
    $pdo = new PDO($dbConn,$dbUser,$dbPass);
    } catch (PDOException $e){
        echo 'Connection failed: ' . $e->getMessage();
    }
$statement = 'INSERT INTO dashboard.dashboard (studyName, respondent_id1, respondent_id2, respondent_id3, sample_provider, completion_status, loi, [endpoint]) VALUES (:studyName, :respondent_id1, :respondent_id2, :respondent_id3, :sample_provider, :completion_status, :loi, :ep);';
$result = $pdo->prepare($statement);
$arr = array('studyName' => $studyName, 'respondent_id1' => $respondent_id1, 'respondent_id2' => $respondent_id2, 'respondent_id3' => $respondent_id3, 'sample_provider' => $sample_provider, 'completion_status' => $completion_status, 'loi' => $loi, 'ep' => $endpoint);
$result->execute($arr);

I know I am not having issues with the database connection, as the query works with hard-coded values, just not with a prepared statement.

For reference, if I replace $statement with the following it works fine:

$statement = "INSERT INTO dashboard.dashboard (studyName, respondent_id1, respondent_id2, respondent_id3, sample_provider, completion_status, loi, [endpoint]) VALUES ('testingNewPush', 'test', 'test', 'test', 'provEx', 'terminate', 500, 'testing');";

In addition, I am receiving the following output when I var_dump($arr):

array(8) { ["studyName"]=> NULL ["respondent_id1"]=> string(5) "test2" ["respondent_id2"]=> string(4) "test" ["respondent_id3"]=> string(4) "test" ["sample_provider"]=> string(10) "Communicus" ["completion_status"]=> string(9) "terminate" ["loi"]=> int(600) ["ep"]=> string(3) "Age" }

I would really appreciate any insight into what I may be doing wrong here.

  • 写回答

1条回答 默认 最新

  • dongyue3795 2018-08-27 05:03
    关注

    In array need to write ":" array(':studyName' => $studyName, ':respondent_id1' => $respondent_id1)

    $statement = 'INSERT INTO dashboard.dashboard (studyName, respondent_id1, respondent_id2, respondent_id3, sample_provider, completion_status, loi, [endpoint]) VALUES (:studyName, :respondent_id1, :respondent_id2, :respondent_id3, :sample_provider, :completion_status, :loi, :ep);';
        $result = $pdo->prepare($statement);
        $arr = array(':studyName' => $studyName, ':respondent_id1' => $respondent_id1, ':respondent_id2' => $respondent_id2, ':respondent_id3' => $respondent_id3, ':sample_provider' => $sample_provider, ':completion_status' => $completion_status, ':loi' => $loi, ':ep' => $endpoint);
        $result->execute($arr);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来