For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > June 2006 > Fwd: MDB2 - getAfterID raises "could not get last insert ID"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Fwd: MDB2 - getAfterID raises "could not get last insert ID"
LFaR

2006-06-25, 8:00 am

(Apologies - I incorrectly posted to pear-dev - I hope this list
is correct, please direct me if not)

Hello keen MDB2 fans,

After using autoExecute to insert a new record into a table, I have not been
able to extract the last ID that was inserted. What could I be doing wrong?

Some of the details:

$affected = $this->dao->db->extended->autoExecute(
$this->table_name, $post, $autoquery, $where, null);
This succeeds. Immediately after this I issue:

echo '<pre>_iud Last ID: ';
print_r($this->dao->db->extended->getAfterId(0, null, null));
echo '</pre>';
This raises the error:


MDB2 Error: Array: lastInsertID: Could not get last insert ID[Native code: 0]



I have verified that lastInsertID is returning 0 (i.e. not false, but
actually the value
'0'). From the docs, it states this returns *0* if the previous query
does not generate an AUTO_INCREMENT value. How is it that MDB2 is not
causing an AUTO_INCREMENT, where native access to the database does?



E.g. if I use the native access to mssql, e.g.




$query = "insert into receipt (amount) values ('11.00')";
$result = mysql_db_query($this->dbname, $query, $this->link);

$lastId = mysql_insert_id($this->link);

echo 'Last ID:' . $lastId . "\n";

This works as expected, and the last ID is displayed.



How can I get this information from MDB2 using autoExecute queries?
What am I doing wrong?



To help with even more detail, I have strategically inserted some
'print_r' statements in the
'Extended.php' module. Output is attached below.



Any hints much appreciated.



Regards,

LFaR.

----------------------------------------

Here are the results of that:



autoExecute($table, $fields_values, $mode, $where,
$types,$result_class): (receipt,Array
(
[amount] => 1800.00
)
,1,,)

mdb2_statement_mysql Object
(
[db] => mdb2_driver_mysql Object
(
[_debug] =>
[_default_error_mode] =>
[_default_error_options] =>
[_default_error_handler] =>
[_error_class] => PEAR_Error
[_expected_errors] => Array
(
)

[db_index] => 1
[dsn] => Array
(
[phptype] => mysql
[dbsyntax] => mysql
[username] => root
[password] => secret
[protocol] => tcp
[hostspec] => localhost
[port] =>
[socket] =>
[database] =>
[mode] =>
)

[connected_dsn] => Array
(
[phptype] => mysql
[dbsyntax] => mysql
[username] => root
[password] => secret
[protocol] => tcp
[hostspec] => localhost
[port] =>
[socket] =>
[database] =>
[mode] =>
)

[connection] => Resource id #42
[opened_persistent] =>
[database_name] => qgw
[connected_database_name] => qgw
[connected_server_info] => 5.0.18-nt-log
[supported] => Array
(
[sequences] => emulated
[indexes] => 1
[affected_rows] => 1
[summary_functions] => 1
[order_by_text] => 1
[transactions] =>
[current_id] => emulated
[limit_queries] => 1
[LOBs] => 1
[replace] => 1
[sub_selects] => 1
[auto_increment] => 1
[primary_key] => 1
[result_introspection] => 1
[prepared_statements] => 1
)

[options] => Array
(
[ssl] =>
[field_case] => 0
[disable_query] =>
[result_class] => MDB2_Result_%s
[buffered_result_class] => MDB2_BufferedResult_%s
[result_wrap_class] =>
[result_buffering] => 1
[fetch_class] => stdClass
[persistent] =>
[debug] => 0
[debug_handler] => MDB2_defaultDebugOutput
[default_text_field_length] => 4096
[lob_buffer_length] => 8192
[log_line_break] =>

[idxname_format] => %s_idx
[seqname_format] => %s_seq
[seqcol_name] => sequence
[quote_identifier] =>
[use_transactions] =>
[decimal_places] => 2
[portability] => 127
[modules] => Array
(
[ex] => Extended
[dt] => Datatype
[mg] => Manager
[rv] => Reverse
[na] => Native
[fc] => Function
)

[emulate_prepared] =>
[default_table_type] =>
)

[escape_quotes] => \
[warnings] => Array
(
)

[debug_output] =>
[in_transaction] =>
[offset] => 0
[limit] => 0
[phptype] => mysql
[dbsyntax] => mysql
[last_query] => PREPARE
MDB2_Statement_mysqld2984bd77cde3848df0a
c7f981fe288b FROM 'INSERT INTO
receipt (amount) VALUES (?)'
[fetchmode] => 2
[modules] => Array
(
[Extended] => mdb2_extended Object
(
[db_index] => 1
)

[Manager] => mdb2_driver_manager_mysql Object
(
[verified_table_types] => Array
(
)

[db_index] => 1
)

[Reverse] => mdb2_driver_reverse_mysql Object
(
[db_index] => 1
)

[Datatype] => mdb2_driver_datatype_mysql Object
(
[db_index] => 1
[valid_types] => Array
(
[text] =>
[boolean] => 1
[integer] => 0
[decimal] => 0
[float] => 0
[timestamp] => 1970-01-01 00:00:00
[time] => 00:00:00
[date] => 1970-01-01
[clob] =>
[blob] =>
)

[lobs] => Array
(
)

)

)

[destructor_registered] =>
[extended] => mdb2_extended Object
(
[db_index] => 1
)

[manager] => mdb2_driver_manager_mysql Object
(
[verified_table_types] => Array
(
)

[db_index] => 1
)

[loaded_version_modules] => Array
(
[0] => manager
[1] => reverse
[2] => datatype
)

[reverse] => mdb2_driver_reverse_mysql Object
(
[db_index] => 1
)

[datatype] => mdb2_driver_datatype_mysql Object
(
[db_index] => 1
[valid_types] => Array
(
[text] =>
[boolean] => 1
[integer] => 0
[decimal] => 0
[float] => 0
[timestamp] => 1970-01-01 00:00:00
[time] => 00:00:00
[date] => 1970-01-01
[clob] =>
[blob] =>
)

[lobs] => Array
(
)

)

)

[statement] => MDB2_Statement_mysqld2984bd77cde3848df0a
c7f981fe288b
[query] => PREPARE
MDB2_Statement_mysqld2984bd77cde3848df0a
c7f981fe288b FROM 'INSERT INTO
receipt (amount) VALUES (?)'
[result_types] => Array
(
[0] =>
)

[types] => Array
(
)

[values] => Array
(
)

[limit] => 0
[offset] => 0
[is_manip] => 1
[positions] => Array
(
[0] => 37
)

)

_iud Last ID:

*QGW PEAR-Error*
MDB2 Error: Array: lastInsertID: (0) Could not get last insert
ID[Native code: 0]
[Native message: ]

mdb2_error Object ( [error_message_prefix] => [mode] => 16 [level] => 1024
[code] => [message] => MDB2 Error: Array [userinfo] => lastInsertID: (0)
Could not get last insert ID[Native code: 0] [Native message: ] [backtrace]
=> Array ( [0] => Array ( [file] => c:\php-4.4.2-Win32\PEAR\MDB2.php [line]
=> 951 [function] => pear_error [class] => mdb2_error [type] => -> [args] =>
Array ( [0] => MDB2 Error: Array [1] => [2] => 16 [3] =>
qgw_handle_pear_error [4] => lastInsertID: (0) Could not get last insert
ID[Native code: 0] [Native message: ] ) ) [1] => Array ( [file] => c:\php-
4.4.2-Win32\PEAR\PEAR.php [line] => 540 [function] => mdb2_error [class] =>
mdb2_error [type] => -> [args] => Array ( [0] => [1] => 16 [2] =>
qgw_handle_pear_error [3] => lastInsertID: (0) Could not get last insert
ID[Native code: 0] [Native message: ] ) ) [2] => Array ( [file] => c:\php-
4.4.2-Win32\PEAR\MDB2.php [line] => 1357 [function] => raiseerror [class] =>
pear [type] => :: [args] => Array ( [0] => [1] => [2] => [3] => [4] =>
lastInsertID: (0) Could not get last insert ID[Native code: 0] [Native
message: ] [5] => MDB2_Error [6] => 1 ) ) [3] => Array ( [file] => c:\php-
4.4.2-Win32\PEAR\MDB2\Driver\mysql.php [line] => 917 [function] =>
raiseerror [class] => mdb2_driver_mysql [type] => -> [args] => Array ( [0]
=> [1] => [2] => [3] => lastInsertID: (0) Could not get last insert ID ) )
[4] => Array ( [file] => c:\php-4.4.2-Win32\PEAR\MDB2\Extended.php [line] =>
621 [function] => lastinsertid [class] => mdb2_driver_mysql [type] => ->
[args] => Array ( [0] => [1] => ) ) [5] => Array ( [file] => C:\Documents
and Settings\Matt\My Documents\My Websites\qgw-
0.11\private\app\php\lib\table_class.php [line] => 296 [function] =>
getafterid [class] => mdb2_extended [type] => -> [args] => Array ( [0] => 0
[1] => [2] => ) ) [6] => Array ( [file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0.11\private\app\php\lib\table_class.php [line] =>
308 [function] => _iud [class] => receipt [type] => -> [args] => Array ( [0]
=> [1] => 1 ) ) [7] => Array ( [file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0. 11\private\app\php\actions\action_confir
m.php[line]
=> 16 [function] => insert [class] => receipt [type] => -> [args] =>
Array ( ) ) [8] => Array ( [file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0.11\private\app\php\lib\bookingfsm_class.php[line]
=> 116 [function] => action [args] => Array ( ) ) [9] => Array (
[file] => C:\Documents and Settings\Matt\My Documents\My Websites\qgw-
0. 11\private\app\php\controllers\book_guid
ed_walk.php [line] => 21
[function] => next_state [class] => bookingfsm [type] => -> [args] => Array
( [0] => Confirm ) ) [10] => Array ( [file] => C:\Documents and
Settings\Matt\My Documents\My Websites\qgw-0.11\index.php [line] => 74
[args] => Array ( [0] => C:\Documents and Settings\Matt\My Documents\My
Websites\qgw-0. 11\private\app\php\controllers\book_guid
ed_walk.php )
[function] => include ) ) [callback] => qgw_handle_pear_error )

*QGW PEAR-Error*
MDB2 Error: Array: lastInsertID: (0) Could not get last insert
ID[Native code: 0]
[Native message: ]

Action Confirm Last ID:mdb2_error Object
(
[error_message_prefix] =>
[mode] => 16
[level] => 1024
[code] =>
[message] => MDB2 Error: Array
[userinfo] => lastInsertID: (0) Could not get last insert ID[Native code: 0]
[Native message: ]

[backtrace] => Array
(
[0] => Array
(
[file] => c:\php-4.4.2-Win32\PEAR\MDB2.php
[line] => 951
[function] => pear_error
[class] => mdb2_error
[type] => ->
[args] => Array
(
[0] => MDB2 Error: Array
[1] =>
[2] => 16
[3] => qgw_handle_pear_error
[4] => lastInsertID: (0) Could not get
last insert ID[Native code: 0]
[Native message: ]

)

)

[1] => Array
(
[file] => c:\php-4.4.2-Win32\PEAR\PEAR.php
[line] => 540
[function] => mdb2_error
[class] => mdb2_error
[type] => ->
[args] => Array
(
[0] =>
[1] => 16
[2] => qgw_handle_pear_error
[3] => lastInsertID: (0) Could not get
last insert ID[Native code: 0]
[Native message: ]

)

)

[2] => Array
(
[file] => c:\php-4.4.2-Win32\PEAR\MDB2.php
[line] => 1357
[function] => raiseerror
[class] => pear
[type] => ::
[args] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => lastInsertID: (0) Could not get
last insert ID[Native code: 0]
[Native message: ]

[5] => MDB2_Error
[6] => 1
)

)

[3] => Array
(
[file] => c:\php-4.4.2-Win32\PEAR\MDB2\Driver\mysql.php
[line] => 917
[function] => raiseerror
[class] => mdb2_driver_mysql
[type] => ->
[args] => Array
(
[0] =>
[1] =>
[2] =>
[3] => lastInsertID: (0) Could not get
last insert ID
)

)

[4] => Array
(
[file] => c:\php-4.4.2-Win32\PEAR\MDB2\Extended.php
[line] => 621
[function] => lastinsertid
[class] => mdb2_driver_mysql
[type] => ->
[args] => Array
(
[0] =>
[1] =>
)

)

[5] => Array
(
[file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0.11\private\app\php\lib\table_class.php
[line] => 424
[function] => getafterid
[class] => mdb2_extended
[type] => ->
[args] => Array
(
[0] => 0
[1] =>
[2] =>
)

)

[6] => Array
(
[file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0. 11\private\app\php\actions\action_confir
m.php
[line] => 17
[function] => getlastid
[class] => receipt
[type] => ->
[args] => Array
(
)

)

[7] => Array
(
[file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0.11\private\app\php\lib\bookingfsm_class.php
[line] => 116
[function] => action
[args] => Array
(
)

)

[8] => Array
(
[file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0. 11\private\app\php\controllers\book_guid
ed_walk.php
[line] => 21
[function] => next_state
[class] => bookingfsm
[type] => ->
[args] => Array
(
[0] => Confirm
)

)

[9] => Array
(
[file] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0.11\index.php
[line] => 74
[args] => Array
(
[0] => C:\Documents and Settings\Matt\My
Documents\My Websites\qgw-0. 11\private\app\php\controllers\book_guid
ed_walk.php
)

[function] => include
)

)

[callback] => qgw_handle_pear_error
)

Lukas Smith

2006-06-26, 7:01 pm

LFaR wrote:
> (Apologies - I incorrectly posted to pear-dev - I hope this list
> is correct, please direct me if not)
>
> Hello keen MDB2 fans,
>
> After using autoExecute to insert a new record into a table, I have not
> been
> able to extract the last ID that was inserted. What could I be doing
> wrong?
>
> Some of the details:
>
> $affected = $this->dao->db->extended->autoExecute(
> $this->table_name, $post, $autoquery, $where, null);
> This succeeds. Immediately after this I issue:
>
> echo '<pre>_iud Last ID: ';
> print_r($this->dao->db->extended->getAfterId(0, null, null));
> echo '</pre>';
> This raises the error:
>
>
> MDB2 Error: Array: lastInsertID: Could not get last insert ID[Native
> code: 0]


hmm not sure .. this works fine in my tests.
btw: as you may notice from the error message: internally MDB2 uses
lastInsertID() for RDBMS that support autoincrement.

so you could also call for slightly better performance:
$this->dao->db->lastInsertID();

I have heard unconfirmed reports about issues with autoincrement and
MDB2 with mysqli. Not sure if there is some buggy version of MySQL on
the loose. What version of MySQL are you running?

If you are sure you are not doing something wrong, please file a bug
report. Optionally this should include a script that I can run that
causes the above mentioned issues on your system.

regards,
Lukas
LFaR

2006-06-28, 8:01 am

Lukas Smith wrote:

>so you could also call for slightly better performance:
>$this->dao->db->lastInsertID();


Thanks for the tip. I have modified my code accordingly.

I will submit a bug report with an example. Of course there is always the
chance that in the process of building a simplified example that I may
discover a problem in my own code (shock and horror! :-).

Best wishes.
LFaR.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com