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

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
RUSS_EMIAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkna_gunnConnect With a Mentor Commented:
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
 
jkna_gunnCommented:
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
 
BAlexandrovCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RUSS_EMIAuthor Commented:
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
 
madwaxCommented:
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
 
RUSS_EMIAuthor Commented:
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
 
madwaxCommented:
Do you simply mean

SELECT id FROM table WHERE book=KnownBookValue

or did I misunderstand the question? =/

//jan
0
 
RUSS_EMIAuthor Commented:
That's it.  But  can't seem to get ID number into a variable (ie. $BID)
Russ
0
 
RUSS_EMIAuthor Commented:
Thank you.  I'll assume that will work.  I hadn't done the reference to [0].
Russ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.