dongwa3808 2015-09-12 07:44
浏览 39
已采纳

HY093:参数数量错误 - 位置

I am getting the HY093 error on the $q->execute line.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'

$stmt = "INSERT INTO `survey`(`user`,`notes`,`lat`,`lon`,`acc`,`timestampx`) VALUES(?,?,?,?,?,?)";
$q = sql::$db->prepare($stmt);
var_dump($data);
$q -> execute($data);

and my vardump echos:

array(6) {
   ["user"]=>string(9) "Your Name"
   ["notes"]=>string(5) "Notes"
   ["lat"]=>string(10) "35.1338614"
   ["lon"]=>string(19) "-106.64091979999999"
   ["acc"]=>string(4) "8512"
   ["time"]=>string(13) "1442043552884"
}

When I copy and paste this data into MySQL (replacing the ? with the quoted strings) it works.

Any idea what I might be missing?

  • 写回答

1条回答 默认 最新

  • duanhua5523 2015-09-12 07:57
    关注

    You are mixing the two ways of working with PHP. If you want to use an associative array to supply the bind values, your query needs to reference them by name, with a colon (:) to indicate these are bind variables:

    $stmt = "INSERT INTO `survey`" .
            "(`user`,`notes`,`lat`,`lon`,`acc`,`timestampx`) " . 
            "VALUES(:user, :notes, :lat, :lon, :acc, :time)";
    

    Alternatively, you could leave $query as is and provide the parameters in a simple, positional, array:

    $data = array("Your Name", 
                  "Notes", 
                  "35.1338614", 
                  "-106.64091979999999", 
                  "8512",
                  "1442043552884");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?