Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General error: 25 bind or column index out of range

Tags:

sqlite

pdo

Using SQLite and PDO. I'm quite new to PDO so I could use some help here. I get the error:

SQLSTATE[HY000]: General error: 25 bind or column index out of range

Here is my code:

// 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);
like image 971
Err Avatar asked Feb 22 '14 22:02

Err


1 Answers

You bind :added, but did not specify it in the VALUES list: (:title,:desc,:album,:version,:cost,:date). Instead you specified :date which is not bound by your array.

like image 52
Ulrich Thomas Gabor Avatar answered Oct 15 '22 07:10

Ulrich Thomas Gabor