MySQL Php get id question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattmcb
Forum Commoner
Posts: 27
Joined: Sun Jan 25, 2004 3:34 pm

MySQL Php get id question

Post by mattmcb »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Let's say you just inserted a new row of information and your id or primary key is auto incrementing... how can you then grad that id to use in later scipts to perform other processes and logic?

Here is an example:

Code: Select all

// Insert data into database
$sql = 'INSERT INTO `picks` ( `team_name` , `team_location` , '.$bet.' , `bet_desc` , `bet_weight` , `comments` , `game_date` , `modify_date` ) '
			. ' VALUES ( '''.$_POST['team_name'].''', '''.$_POST['team_location'].''', '''.$_POST['play'].''', '''.$_POST['play_desc'].''', '''.$_POST['weight'].''', '''.$_POST['comments'].''', '''.$_POST['game_date'].''', NOW( ) );'
			. ' ';
$result = mysql_query($sql)or die (mysql_error());
What I need is the pick_id... anyone know the best way to get it?


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]mysql_insert_id[/php_man]()
mattmcb
Forum Commoner
Posts: 27
Joined: Sun Jan 25, 2004 3:34 pm

Post by mattmcb »

How do you then find out what that id is?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't quite understand what you're asking now.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

[php_man]mysql_fetch_assoc[/php_man] ???
d_d
Forum Commoner
Posts: 33
Joined: Wed Jul 07, 2004 4:56 pm
Location: UK

Post by d_d »

Like feyd said mysql_insert_id
http://www.php.net/mysql_insert_id/
returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier

Code: Select all

<?php
// Insert data into database
$sql = 'INSERT INTO `picks` ( `team_name` , `team_location` , '.$bet.' , `bet_desc` , `bet_weight` , `comments` , `game_date` , `modify_date` ) '
            . ' VALUES ( '''.$_POST['team_name'].''', '''.$_POST['team_location'].''', '''.$_POST['play'].''', '''.$_POST['play_desc'].''', '''.$_POST['weight'].''', '''.$_POST['comments'].''', '''.$_POST['game_date'].''', NOW( ) );'
            . ' ';
$result = mysql_query($sql)or die (mysql_error());
$id = mysql_insert_id();
?>
Post Reply