[ciapug] mysqli prepared statements

Carl Olsen carl-olsen at mchsi.com
Mon Dec 19 08:44:28 CST 2005


YES!!! It $works->

					$stmt->execute();
					$rowsAffected =
$stmt->affected_rows;
					$insertid = $stmt->insert_id;
					$stmt->close();

-----Original Message-----
From: ciapug-bounces at cialug.org [mailto:ciapug-bounces at cialug.org] On Behalf
Of Tim Champion
Sent: Tuesday, December 13, 2005 8:31 AM
To: ciapug at cialug.org
Subject: Re: [ciapug] mysqli prepared statements

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
_______________________________________________
ciapug mailing list
ciapug at cialug.org
http://cialug.org/mailman/listinfo/ciapug



More information about the ciapug mailing list