If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQL Php get id question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-02-04, 00:54
mattmcb mattmcb is offline
Registered User
 
Join Date: Aug 2004
Posts: 4
MySQL Php get id question

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:
// 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?
Reply With Quote
  #2 (permalink)  
Old 10-02-04, 01:52
snorp snorp is offline
Registered User
 
Join Date: Apr 2004
Location: Europe->Sweden->Stockholm
Posts: 71
I assume that pick_id is the primary key.
Have a look at the function called mysql_insert_id(). More info on http://se2.php.net/manual/en/functio...-insert-id.php (it was PHP you were using, right?)

Hope this was what you wanted
Reply With Quote
  #3 (permalink)  
Old 10-02-04, 01:59
snorp snorp is offline
Registered User
 
Join Date: Apr 2004
Location: Europe->Sweden->Stockholm
Posts: 71
By the way, if you insert many rows at the same time, mysql_insert_id() will return the number of the first of the inserted rows.

(Suppose you have 10 rows in your table and do INSERT tab VALUES(...), (...). You will get the return value 11 from mysql_insert_id() since the rows you inserted were 11 and 12.)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On