php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44302 mysqli_insert_id results
Submitted: 2008-03-01 07:27 UTC Modified: 2008-03-11 01:00 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: shaneperc at gmail dot com Assigned:
Status: No Feedback Package: MySQLi related
PHP Version: 5.2.5 OS: Linux 2.6.9
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-03-01 07:27 UTC] shaneperc at gmail dot com
Description:
------------
$mysqli->insert_id is returning nothing. I tried mysqli_insert_id($mysqli) as well with the same result. You've heard this before, BUT, I found a reproducible fix. I found that by concatenating an empty string to the scalar that holds the id, it's then accessible.

In the code below, if the commented line is uncommented, everything works as expected. Without adding the empty string, it doesn't work. (and testing the value doesn't work at all).

I hope this is helpful to someone who can fix this.

Reproduce code:
---------------
$id = (is_numeric($_POST['staff_id'])) ? $_POST['staff_id'] : "new";
if($id == "new"){
   if(!$db->query("INSERT INTO staff() VALUES()")){
	die("Couldn't insert a new staffer: " . $db->error);
   }
   $id = $db->insert_id;
   if(!$id){
      die("<h2>IDs are acting up again! Tell Shane and don't use this again until further notice</h2>");
   }
}

/*$id = $id . "";  -----uncommenting this will make it work-----*/
die($id);


Expected result:
----------------
Expect to see ID number (either one already set, or the newly generated one). BTW, if $_POST['staff_id'] doesn't require the insert, this works everytime as expected.

Actual result:
--------------
Nothing on screen, nothing in the generated source.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-03 12:57 UTC] jani@php.net
What does var_dump($id); output in that case? Or var_dump($db->insert_id); ?
 [2008-03-11 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Aug 09 19:04:05 2022 UTC