[ciapug] mysqli prepared statements

Tim Champion timchampion at bigfoot.com
Tue Dec 13 08:30:38 CST 2005


Maybe I'm missing something here, but have you tried the PHP function:
mysql_insert_id()?
http://us3.php.net/mysql_insert_id


On 12/12/05, Dave J. Hala Jr. <dave at 58ghz.net> wrote:
> 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
>
> _______________________________________________
> ciapug mailing list
> ciapug at cialug.org
> http://cialug.org/mailman/listinfo/ciapug
>


--
Tim Champion
timchampion at bigfoot.com


More information about the ciapug mailing list