php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8101 mysql_insert_id
Submitted: 2000-12-04 14:23 UTC Modified: 2001-01-30 04:20 UTC
From: tetherow at nol dot org Assigned:
Status: Closed Package: MySQL related
PHP Version: 4.0.3pl1 OS: Linux 2.2.16 (Debian)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tetherow at nol dot org
New email:
PHP Version: OS:

 

 [2000-12-04 14:23 UTC] tetherow at nol dot org
MySQL insert_id function returns 0 on valid insert.

Using PHP 4.0.3pl1 and MySQL 3.23.28gamma.
configured using --with-mysql=/usr/local

$connection=mysql_pconnect($server, $username, $password);
$cursor=mysql_db_query('staff', $query, $connection);
$id=mysql_insert_id();
have also tried
$id=mysql_insert_id($connection)

In both instances $id is 0 yet the insert does succeed. 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-12-05 08:25 UTC] sniper@php.net
What is the table like in which your query inserts data?
And what is the query like? 
And have you read the following page?

http://www.php.net/manual/function.mysql-insert-id.php

--Jani
 [2000-12-07 10:47 UTC] tetherow at nol dot org
mysql> show columns from staff; 
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      |      | PRI | NULL    | auto_increment |
| firstname | varchar(80)  | YES  |     | NULL    |                |
| lastname  | varchar(80)  | YES  |     | NULL    |                |
| title     | varchar(80)  | YES  |     | NULL    |                |
| phone     | varchar(15)  | YES  |     | NULL    |                |
| cell      | varchar(15)  | YES  |     | NULL    |                |
| home      | varchar(15)  | YES  |     | NULL    |                |
| pager     | varchar(60)  | YES  |     | NULL    |                |
| email     | varchar(30)  | YES  |     | NULL    |                |
| username  | varchar(16)  | YES  |     | NULL    |                |
| password  | varchar(16)  | YES  |     | NULL    |                |
| epassword | varchar(16)  | YES  |     | NULL    |                |
| company   | int(11)      | YES  |     | NULL    |                |
| role      | varchar(80)  | YES  |     | NULL    |                |
| other     | varchar(15)  | YES  |     | NULL    |                |
| active    | tinyint(4)   | YES  |     | NULL    |                |
| shell     | varchar(128) | YES  |     | NULL    |                |
| forward   | varchar(64)  | YES  |     | NULL    |                |
| disporder | int(11)      | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+

 $query="insert into staff (id, firstname, lastname, title, phone, cell, home, pager, email, username, password, epassword, company, role, other, active, shell, forward, disporder) values (NULL, '$this->firstname', '$this->lastname', '$this->title', '$this->phone', '$this->cell', '$this->home', '$this->pager', '$this->email', '$this->username', '$this->password', '$this->epassword', '$this->company', '$this->role', '$this->other', '1', '$this->shell', '', $this->disporder)";
 $cursor=mysql_db_query('staff', $query, $connection);
                // Need to check return here.
                $this->id=mysql_insert_id($connection);
echo  "Connection: $connection<br>\nID: $this->id<br>\n";

When run $connection is set to a valid resource and $this->id is 0.

The data does get inserted into the database without any problems so I know the query is good.

I looked at the page referenced and the auto_increment field is a standard int field.   The LAST_INSERT_ID() function appears to work but am curious as to why the mysql_insert_id does not.
 [2000-12-07 18:31 UTC] sniper@php.net
Why do you have the 'id' in your SQL line? You don't need to mention
it at all, it's 'auto_increment'. ie. it will be automatically incremented.

Try leaving it out. mysql_insert_id() should work then.


--Jani
 [2001-01-30 04:20 UTC] sniper@php.net
No feedback and an user error.

--Jani

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 10 23:01:29 2025 UTC