When using PHP PDO, if you encounter an error like the following:
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 25 bind or column index out of range
Then, it likely means that you are binding more values than the parameters specified in the query. If such is the case, then the statement (whether it's
UPDATE or others) will fail and an error will be emitted.
To demonstrate this, let's suppose you have a
customer table like the following:
$db = new PDO('sqlite::memory'); $schema =<<<SQL CREATE TABLE `customer` ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(25) NOT NULL, date_birth DATE NOT NULL ); SQL; $db->exec($schema);
To reproduce the error, let's assume that you're binding an extra parameter to the query called "
$query =<<<SQL INSERT INTO `customer` (name, date_birth) VALUES (:name, :date_birth) SQL; $sth = $db->prepare($query); $boundedArgs = [ 'name' => 'John', 'date_birth' => '2000-10-10', // Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 25 bind or column index out of range 'extra' => '', ]; $sth->execute($boundedArgs);
To fix this error, you simply need to make sure that the bound parameters are not more than what you are expecting in your query. You can do either of the following:
- Add the missing parameter name to the query, or;
- Remove the extra parameter from the bounded parameters.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.