Solved

Newbie simple question - how do I get the ID of a record I've just inserted into the table in mysql using php?

Posted on 2004-04-13
9
340 Views
Last Modified: 2013-12-12
I need the ID, so I can UPDATE other associated tables.

The book_table contains
ID key unique
Count
Book   unique
Description

The value I know is $NewBook
Thanks,
Russ
0
Comment
Question by:RUSS_EMI
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10811410
mysql_query("INSERT INTO books (id,count,book,description) values (null,5,'my book','blah blah blah')");
printf("Last inserted record id %d", mysql_insert_id());

i assume that your ID is autoincrement in which case i just pass in null to the insert query.
0
 
LVL 6

Expert Comment

by:BAlexandrov
ID: 10812375
Is this working in mysql? Passing null value of autoincrement field? I think this must be done with omitting of id field from list of fields.
0
 

Author Comment

by:RUSS_EMI
ID: 10812478
Jkna_gunn,
   Thank you, that works. But I also need the ID for an existing record where I know the $Book value.  Is that done the similarly, but using mysql_select_id() (after I do a SELECT query)?
Thanks
Russ
0
 
LVL 7

Expert Comment

by:madwax
ID: 10812646
Otherwise you can always get the next auto_index by the following statements

--code--

$sql = "SHOW TABLE STATUS LIKE \"tableName\"";
$result=execute($sql);
$rs=mysql_fetch_object($result);
mysql_free_result($result);
$nextID=$rs->Auto_increment;

--code--

The problem is that you have to lock the table before so that an other query doesn't take this id - I.E. unsuitable when there is a lot of inserting at the same time...

//jan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RUSS_EMI
ID: 10812722
What I need, is to get an old, pre-existing ID value from a record when I know the unique Book value, but nothing else.
Russ
0
 
LVL 7

Expert Comment

by:madwax
ID: 10812856
Do you simply mean

SELECT id FROM table WHERE book=KnownBookValue

or did I misunderstand the question? =/

//jan
0
 

Author Comment

by:RUSS_EMI
ID: 10812974
That's it.  But  can't seem to get ID number into a variable (ie. $BID)
Russ
0
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 125 total points
ID: 10813240
are you sure the select query is returning anything?

$query = "select id from book_table where book='$book'";

$result = mysql_query($query);

$row = mysql_fetch_array($result);

$id = $row[0];

print $id;
0
 

Author Comment

by:RUSS_EMI
ID: 10813508
Thank you.  I'll assume that will work.  I hadn't done the reference to [0].
Russ
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now