php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54142 the function mysqli_insert_id doesn't return the last id
Submitted: 2011-03-02 22:42 UTC Modified: 2011-03-03 06:08 UTC
From: lissetteganoza at gmail dot com Assigned:
Status: Not a bug Package: PHP options/info functions
PHP Version: 5.2.17 OS: windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lissetteganoza at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-02 22:42 UTC] lissetteganoza at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/mysqli.insert-id#DescripciĆ³n
---

The function mysqli_insert_id  does not return the if of the last insert. The 
query was executed without problem but I can get the id of this insert. I want to 
konw if exist other function other possiblity, maybe I need to reconfigure some 
value in the php ini, but I need a solution and this solution must be in the 
documents of this funcionality.
I'm adding the function that I have for insert a row.
 

Test script:
---------------
function insert($ins)
	{
		$result = @mysqli_query($this->conn,$ins);

		if (!$result)
		{
			$this->writeTemplate(DB_TITULO_ERROR,mysqli_error());
		}

		return @mysqli_insert_id($this->conn);
	}

Expected result:
----------------
- A solution to use correctly this function
- Id does not exist a solution with this function, another solution with other 
functions of mysql

Actual result:
--------------
the function returns a empty object.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-03 06:08 UTC] dtajchreber@php.net
-Status: Open +Status: Bogus
 [2011-03-03 06:08 UTC] dtajchreber@php.net
david@copenhagen:~/test$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 64
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> describe test;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| pid   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| n_pid | date             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> select * from test;
+-----+------------+
| pid | n_pid      |
+-----+------------+
|   1 | 2011-03-02 |
+-----+------------+
1 row in set (0.00 sec)

mysql> exit
Bye
david@copenhagen:~/test$ cat t.php 
<?php

$l = mysqli_connect('127.0.0.1', 'root', 'root');
var_dump(mysqli_select_db($l, 'test'));
var_dump(mysqli_query($l, 'insert into test (n_pid) values(curdate())'));
var_dump(mysqli_insert_id($l));

david@copenhagen:~/test$ /home/david/dev/php/php53/sapi/cli/php -v
PHP 5.3.6-dev (cli) (built: Feb 26 2011 23:29:38) (DEBUG)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
david@copenhagen:~/test$ /home/david/dev/php/php53/sapi/cli/php t.php 
bool(true)
bool(true)
int(9)
david@copenhagen:~/test$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 66
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from test;
+-----+------------+
| pid | n_pid      |
+-----+------------+
|   1 | 2011-03-02 |
|   9 | 2011-03-02 |
+-----+------------+
2 rows in set (0.00 sec)

mysql> exit
Bye
david@copenhagen:~/test$
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 21:01:28 2024 UTC