[ciapug] mysqli prepared statements

Dave J. Hala Jr. dave at 58ghz.net
Mon Dec 12 16:51:08 CST 2005


You could add a "random number" field to your table. Then generate a
large random number and add to your insert. Then query for the record id
that matches the random number you inserted.

Like this:

INSERT mytable (data,random) values ('$data',$random_number');

Then

SELECT id from mytable WHERE random='$random_number';

Not terribly elegant -but it would work.

:) Dave



On Mon, 2005-12-12 at 16:12, Scott Phillips wrote:
> I think the resounding silence following your question can be translated 
> into, "No. We don't know."
> 
> Regarding your current approach... Do you lock the table before the insert 
> and unlock it after you select the max id? Otherwise, another record could 
> be inserted between the two and you'll end up with the wrong id. That's how 
> I do it, anyway. Is there a better way?
> 
> 
> 
> At 10:26 AM 12/10/2005 -0600, you wrote:
> >Does anyone know if you can use the mysqli->insert_id inside a prepared
> >statement to get the id of the record that has just been inserted?
> >
> >I'm doing it with a select statement which uses the same parameters as the
> >insert statement and selects "MAX(id) AS id" as the output parameter.  It
> >works, but it's easy to make an error when typing it in.
> >
> >Carl
> >http://www.carl-olsen.com/
> >
> >_______________________________________________
> >ciapug mailing list
> >ciapug at cialug.org
> >http://cialug.org/mailman/listinfo/ciapug
> 
> _______________________________________________
> ciapug mailing list
> ciapug at cialug.org
> http://cialug.org/mailman/listinfo/ciapug
-- 

Open Source Information Systems, Inc. (OSIS)
Dave J. Hala Jr., President <dave at osis.us>
641.485.1606



More information about the ciapug mailing list