使用SQLite和PDO。我对PDO很新,所以我可以在这里使用一些帮助。我收到错误:
SQLSTATE[HY000]: General error: 25 bind or column index out of range
这是我的代码:
// db.php
function dbh($sql, $db = "", $nsx = "") {
$db = ($db) ? $db : "mydb";
$db = $db.".sqlite";
try {
$dbh = new PDO("sqlite:$db");
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// id, title, desc, cost, album, version, added
$dbh->exec("CREATE TABLE IF NOT EXISTS `products` (
`id` INTEGER PRIMARY KEY,
`title` TEXT,
`desc` TEXT,
`cost` TEXT,
`album` TEXT,
`version`TEXT,
`added` DATETIME
)");
//$mem = new PDO('sqlite::memory');
//$mem->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$res = $dbh->prepare($sql);
//var_dump($nsx);
$res->execute($nsx);
if (!is_array($nsx)) {
return $res->fetchAll();
}
}
catch(PDOException $e) {
return $e->getMessage();
}
}
// products.php
$sql = "INSERT INTO `products` (title, desc, cost, album, version, added) VALUES (:title,:desc,:album,:version,:cost,:date)";
$nsx = array(
':title' => $i['addon']['title'][$k],
':desc' => $i['addon']['desc'][$k],
':cost' => $i['addon']['cost'][$k],
':album' => $album,
':version' => '1.0',
':added' => $date,
);
$dbh = dbh($sql,"",$nsx);
答案 0 :(得分:17)
绑定:added
,但未在VALUES列表中指定它:(:title,:desc,:album,:version,:cost,:date)
。而是指定了不受数组约束的:date
。