I'm flummoxed by this error, even worse is that the code seems to work sometimes but I can't see any pattern in the data that causes it.
The code is as follows:-
try { $insQuery = "INSERT INTO log "; // $insQuery .= "(seqno, year, month, day, hour, minute, waterway, pk, place, distance, eng_hrs, fuel_cm, fuel_lt, note, type, sailing)"; $insQuery .= "(seqno, year, month, day)"; $insQuery .= " VALUES "; // $insQuery .= "(:seqno, :year, :month, :day, :hour, :minute, :waterway, :pk, :place, :distance, :eng_hrs, :fuel_cm, :fuel_lt, :note, :type, :sailing );"; $insQuery .= "(:seqno, :year, :month, :day);"; $st = $db->prepare($insQuery); foreach ($rows as $row) { $row['seqno'] = $seqno++; $st->execute($row); } $st->closeCursor; } catch (PDOException $e) { dbglog("PDO() insert error" . $e->getMessage()); dbglog("insquery: $insQuery"); foreach($row as $k=>$v) { dbglog("k is: $k, v is $v"); } }
... and what I'm seeing in the log is:-
13:53:59 192.168.1.4: PDO() insert errorSQLSTATE[HY000]: General error: 25 bind or column index out of range 13:53:59 192.168.1.4: insquery: INSERT INTO log (seqno, year, month, day) VALUES (:seqno, :year, :month, :day); 13:53:59 192.168.1.4: k is: year, v is 2011 13:53:59 192.168.1.4: k is: month, v is 11 13:53:59 192.168.1.4: k is: day, v is 12 13:53:59 192.168.1.4: k is: hour, v is 10 13:53:59 192.168.1.4: k is: minute, v is 00e 13:53:59 192.168.1.4: k is: waterway, v is 13:53:59 192.168.1.4: k is: pk, v is 13:53:59 192.168.1.4: k is: place, v is Cappy 13:53:59 192.168.1.4: k is: distance, v is 13:53:59 192.168.1.4: k is: eng_hrs, v is 2695.3 13:53:59 192.168.1.4: k is: fuel_cm, v is 8.5 13:53:59 192.168.1.4: k is: fuel_lt, v is 13:53:59 192.168.1.4: k is: note, v is Engine started after replacing blown mega-fuse, all OK otherwise 13:53:59 192.168.1.4: k is: type, v is TEF 13:53:59 192.168.1.4: k is: price, v is 13:53:59 192.168.1.4: k is: sailing, v is 1 13:53:59 192.168.1.4: k is: seqno, v is 0
So what does "General error: 25 bind or column index out of range" actually mean, I could find nowhere that explains it. Now my guess is that it's telling me that I have a column name wrong somewhere but my 'log' table definitely has columns year, month, day and seqno of the correct type. Maybe I have the syntax wrong somehow for the VALUES() bit but again I can't find any really good explanation of exactly what the syntax is, it seems decidedly woolly.
Well, I sat and played with some old[er] code that did work (thank goodness for incremental backups!) and compared it with the change that didn't work and eventually worked out what the problem was.
The $st->execute($row); doesn't like it if $row contains a column that doesn't match the prepared statement. It can't cope with using just the columns it needs from $row.
I had explicitly set a value in $row empty (and thus the value existed) which didn't occur in the prepared statement. Then, trying to cut down the prepared statement simply made things worse because lots of values in $row were set that weren't in the prepared statement.
As always I suspect that explaining the problem made me stand back from it a little and work out what was wrong - so thanks for listening anyway (if you bothered!). :-)
On 27/11/11 15:12, Chris Green wrote:
As always I suspect that explaining the problem made me stand back from it a little and work out what was wrong - so thanks for listening anyway (if you bothered!). :-)
You need a cardboard programmer ;-)
http://c2.com/cgi/wiki?CardboardProgrammer