MySQL Forums
Forum List  »  Newbie

Re: Last/highest ID?
Posted by: Wesley notrelevant
Date: November 27, 2010 12:38PM

David Garstang Wrote:
-------------------------------------------------------
> OK, here's an example PHP fragment from my own
> code. I did a quick clean-up of irrelevant stuff,
> so I hope I didn't break anything, but I don't
> have time to test it now, sorry!
>
> Just above this PHP snipped, I had inserted a new
> row into the "person" table, which has an
> auto-increment ID colum. Here's how I retrieve
> the ID into a PHP variable named $idnum:
>
> ---------------
> $result=mysql_query("SELECT LAST_INSERT_ID();",
> $db_conn);
> if (!$result)
> {
> die ("ID retrieval failed: ".mysql_error());
> }
> $row = mysql_fetch_row($result);
> $idnum = $row[0];
> -----------------------------
>
> The mysql_query function returns an integer value
> that identifies the result set that was generated
> by the query. This goes into $result in the above
> example.
>
> Then mysql_fetch_row fetches a single row from
> this result set into an array, putting it into a
> variable named $row.
>
> "$idnum = $row[0]" returns the first (and in this
> case ONLY) value in that row, which is the desired
> ID. (In case you weren't aware, in PHP arrays,
> the first element is always item ZERO, not one)


Cheers, it worked! Thanks! ;-)

Options: ReplyQuote


Subject
Written By
Posted
November 26, 2010 07:41AM
November 26, 2010 07:48AM
November 26, 2010 07:56AM
November 26, 2010 08:24AM
November 26, 2010 08:29AM
November 26, 2010 08:32AM
November 26, 2010 09:08AM
November 26, 2010 09:35AM
November 26, 2010 11:38AM
November 26, 2010 04:07PM
November 26, 2010 08:40PM
November 27, 2010 04:22AM
November 27, 2010 04:27AM
November 27, 2010 04:37AM
November 27, 2010 04:43AM
November 27, 2010 05:25AM
November 27, 2010 05:44AM
November 27, 2010 05:49AM
Re: Last/highest ID?
November 27, 2010 12:38PM
November 26, 2010 07:48AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.