Current location: Hot Scripts Forums » Other Discussions » Database » PHP/MySQL Question


PHP/MySQL Question

Reply
  #1 (permalink)  
Old 03-15-07, 02:17 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
PHP/MySQL Question

This is a mysql heavy question but i think it has some relevance to php so ill post it here.

I have a few tables that when new data is inserted into them they will have new unique ID number generated for them by mysql... for example

INSERT INTO db_table SET id=null, name="dan", age="23"

Now in php I have always ran 2 queries, one for the insert and the other for getting the ID that mysql has set for the just inserted data. Is there a way that i can get the ID back in a single query in php, maybe like....

PHP Code:

$query='INSERT INTO db_table SET id=null, name="dan", age="23";SELECT id FROM db_table WHERE name="dan" && age="23"';

$result mysql_query($query);
$array mysql_fetch_array($query); 
Would $array['id'] equal the generated ID?

Is there a technique out there I am missing to do this?

I'm just wondering, trying to cut down running unnecessary queries in a script.
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-15-07, 02:21 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
www.php.net/mysql_insert_id
PHP Code:

<?php

mysql_query
"INSERT INTO db_table ( id, name, age ) VALUES ( NULL, 'dan', '23' )";
$id mysql_insert_id();

?>
__________________
The toxic ZCE

Last edited by Keith; 03-15-07 at 02:25 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-15-07, 02:27 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
lol thanks, stupidly simple.
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 03:04 PM
Posting a question / answer on site markcody PHP 2 11-23-04 02:58 PM
Another PHP/MySQL Question Lucas PHP 1 06-25-04 01:27 PM
[PHP] Array question UmiSal Script Requests 1 04-05-04 02:52 PM
question and answer software jaydifox C/C++ 0 02-21-04 10:26 AM


All times are GMT -5. The time now is 12:49 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.