Solved

Get current id from MySQL post/query?

Posted on 2009-05-09
2
443 Views
Last Modified: 2013-12-12
Hi Experts!

In the same PHP file I want to:

1. I want to create a new user and post it to the database (that code is already created)

2. echo the user id for that new user.

How can I do that?

I could find out the highest user id from the database and echo that, but it doesnt feel 100% safe.

Is there a way to find out the "current" id that was posted at that moment?

Thanks in advice!
0
Comment
Question by:GodDoesntExist
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24344086
presuming that your table has auto_increment, learn about the LAST_INSERT_ID() function of mysql:
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

resp: the php shortcut function mysql_insert_id:
http://lu2.php.net/mysql_insert_id

that's 100% safe.
0
 
LVL 3

Author Closing Comment

by:GodDoesntExist
ID: 31579773
Thank you!

Perfect solution!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question