Solved

Get current id from MySQL post/query?

Posted on 2009-05-09
2
451 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 143

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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