dpd2349 2015-10-09 16:25
浏览 73
已采纳

PDOException'参数号无效:未定义参数

I think that this should be a simple problem but I can not see it.

$SQL = "Select `id` from `fileStore` WHERE `uid` = ':UID' and `offset` = :OFFSET";
$InPut = array(':UID' => $LocArr[0],':OFFSET' => $LocArr[1]);
echo(str_replace(array_flip($InPut),$InPut,$SQL));
$stmt = $db->prepare($SQL);
$stmt->execute($InPut);

The Echo line gives me.

Select `id` from `fileStore` WHERE `uid` = '20151009162211-909549588' and `offset` = 1

Which I can run against the database. But I get the error.

Fatal error: 
Uncaught exception 'PDOException' with message 
'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' 
in filepath\UploadParser.php:
117 Stack trace: #0 filepath\UploadParser.php(117): 
PDOStatement->execute(Array) #1 {main} thrown in filepath\UploadParser.php on line 117
  • 写回答

1条回答 默认 最新

  • doupao3662 2015-10-09 17:32
    关注

    In your query you have the first placeholder quoted this makes it a string. When the execute is fired the PDO is confused because you are telling it there are two placeholders but it only finds one (the unquoted one).

    To resolve that try

    $SQL = "Select `id` from `fileStore` WHERE `uid` = :UID and `offset` = :OFFSET";
    $InPut = array(':UID' => $LocArr[0],':OFFSET' => $LocArr[1]);
    $stmt = $db->prepare($SQL);
    $stmt->execute($InPut);
    

    This should make :UID = $LocArr[0] and :OFFSET = $LocArr[1], when it goes to the db.

    It also looks like you are trying to replace the placeholders with the actual values in your query which you shouldn't do. That removes the purpose of prepared statements.

    Another way of writing it would be

    $SQL = "Select `id` from `fileStore` WHERE `uid` = ? and `offset` = ?";
    $stmt = $db->prepare($SQL);
    $stmt->execute(array($LocArr[0], $LocArr[1]));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝