Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 06-10-2007, 12:23 AM   #1
McHenry
 
McHenry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures

The documentation on PHP & stored procedures is limited so can anyone
confirm that the id value of the last inserted row is available via the
"insert_id" function ?

The table uses an AUTO_INCREMENT constraint for the PK column.

I have a sp that is called from PHP to insert a row into a table, after the
procedure has been executed I can obtain db errors, if any, and the number
of rows affected successfully however the insert_id is always 0, even when a
rows has been inserted ?

$leadid=$mysqli->insert_id; //doesn't work
$error = $mysqli->error; //works !
$affected_rows=$stmt->affected_rows; //works !


Thanks in advance...


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 06-10-2007, 12:23 AM   #2
robert
 
robert's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures

| The documentation on PHP & stored procedures is limited so can anyone
| confirm that the id value of the last inserted row is available via the
| "insert_id" function ?

this seems to describe it pretty well:

============ from php manual

The mysqli_insert_id() function returns the ID generated by a query on a
table with a column having the AUTO_INCREMENT attribute. If the last query
wasn't an INSERT or UPDATE statement or if the modified table does not have
a column with the AUTO_INCREMENT attribute, this function will return zero.
Note: Performing an INSERT or UPDATE statement using the LAST_INSERT_ID()
function will also modify the value returned by the mysqli_insert_id()
function.
Return Values
The value of the AUTO_INCREMENT field that was updated by the previous
query. Returns zero if there was no previous query on the connection or if
the query did not update an AUTO_INCREMENT value.
Note: If the number is greater than maximal int value, mysqli_insert_id()
will return a string.

============

now i know your great aversion to suggestion, however it is evident that
such could be helpful to you at this point in your learnin. as you've
noticed, i've cleared out my plonk list...hence this reply. if you behave
and don't take offense, here's a suggestion...

write your code generically such that you aren't strongly tied to any single
technology. many use peardb classes to abstract the code from the db. that
way, the db interface in php uses the same calls/properties/methods no
matter if the db is oracle, terdata, sql server, or mysql. i'm not a huge
fan of peardb but i do take the same approach to abstraction. i have a
simplified db class that works just fine. this is the functionality of its
execute() method:

static function execute($sql, $decode = false, $returnNewId = false)
{
self::$_lastStatement = $sql;
$array = array();
$key = 0;
$records = mysql_query($sql);
$fieldCount = @mysql_num_fields($records);
$translation = get_html_translation_table(HTML_ENTITIES);
$translation = array_flip($translation);
while ($row = @mysql_fetch_array($records, MYSQL_NUM))
{
for ($i = 0; $i < $fieldCount; $i++)
{
$value = $row[$i];
if ($decode){ $value = strtr($value, $translation); }
$array[$key][strtoupper(@mysql_field_name($records, $i))] = $value;
}
$key++;
}
if ($returnNewId)
{
$array = array();
$array[0]['ID'] = mysql_insert_id();
}
@mysql_free_result($records);
return $array;
}

called from code, an example would look like:

$sql = "insert into foo values('bar')";
$records = db::execute($sql, false, true);
echo '<pre>new id = ' . $records[0]['ID'] . '</pre>';

that way, when you upgrade/change databases, you only have to alter the db
class' connect and execute methods...the rest of the scripts simply call the
same interface they always have. now, you can easily take this one step
further in oo design and create an actual db interface and have many of the
db-specific classes implement that interface...then your db upgrade/change
would only need to:

$db = mysql::getInstance();

or

$db = sqlserver::getInstance();

anyway...you will most likely have ignored much of what i posted beyond the
initial php manual description of mysqli->insert_id and how it works. that's
fine...just be civil so i don't have to start re-populating my plonk list.
;^)


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-10-2007, 12:23 AM   #3
McHenry
 
McHenry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures

Thanks you for your help, greatly appreciated.


"robert" <ab@no.spam-alama-ding-dong> wrote in message
news:UGK5g.15$1h4.3@fe06.lga...
>| The documentation on PHP & stored procedures is limited so can anyone
> | confirm that the id value of the last inserted row is available via the
> | "insert_id" function ?
>
> this seems to describe it pretty well:
>
> ============ from php manual
>
> The mysqli_insert_id() function returns the ID generated by a query on a
> table with a column having the AUTO_INCREMENT attribute. If the last query
> wasn't an INSERT or UPDATE statement or if the modified table does not
> have
> a column with the AUTO_INCREMENT attribute, this function will return
> zero.
> Note: Performing an INSERT or UPDATE statement using the LAST_INSERT_ID()
> function will also modify the value returned by the mysqli_insert_id()
> function.
> Return Values
> The value of the AUTO_INCREMENT field that was updated by the previous
> query. Returns zero if there was no previous query on the connection or if
> the query did not update an AUTO_INCREMENT value.
> Note: If the number is greater than maximal int value, mysqli_insert_id()
> will return a string.
>
> ============
>
> now i know your great aversion to suggestion, however it is evident that
> such could be helpful to you at this point in your learnin. as you've
> noticed, i've cleared out my plonk list...hence this reply. if you behave
> and don't take offense, here's a suggestion...
>
> write your code generically such that you aren't strongly tied to any
> single
> technology. many use peardb classes to abstract the code from the db. that
> way, the db interface in php uses the same calls/properties/methods no
> matter if the db is oracle, terdata, sql server, or mysql. i'm not a huge
> fan of peardb but i do take the same approach to abstraction. i have a
> simplified db class that works just fine. this is the functionality of its
> execute() method:
>
> static function execute($sql, $decode = false, $returnNewId = false)
> {
> self::$_lastStatement = $sql;
> $array = array();
> $key = 0;
> $records = mysql_query($sql);
> $fieldCount = @mysql_num_fields($records);
> $translation = get_html_translation_table(HTML_ENTITIES);
> $translation = array_flip($translation);
> while ($row = @mysql_fetch_array($records, MYSQL_NUM))
> {
> for ($i = 0; $i < $fieldCount; $i++)
> {
> $value = $row[$i];
> if ($decode){ $value = strtr($value, $translation); }
> $array[$key][strtoupper(@mysql_field_name($records, $i))] = $value;
> }
> $key++;
> }
> if ($returnNewId)
> {
> $array = array();
> $array[0]['ID'] = mysql_insert_id();
> }
> @mysql_free_result($records);
> return $array;
> }
>
> called from code, an example would look like:
>
> $sql = "insert into foo values('bar')";
> $records = db::execute($sql, false, true);
> echo '<pre>new id = ' . $records[0]['ID'] . '</pre>';
>
> that way, when you upgrade/change databases, you only have to alter the db
> class' connect and execute methods...the rest of the scripts simply call
> the
> same interface they always have. now, you can easily take this one step
> further in oo design and create an actual db interface and have many of
> the
> db-specific classes implement that interface...then your db upgrade/change
> would only need to:
>
> $db = mysql::getInstance();
>
> or
>
> $db = sqlserver::getInstance();
>
> anyway...you will most likely have ignored much of what i posted beyond
> the
> initial php manual description of mysqli->insert_id and how it works.
> that's
> fine...just be civil so i don't have to start re-populating my plonk list.
> ;^)
>
>


I have actually worked around the problem by having the new ID returned form
the procedure as a parameter using:

SET _leadid=LAST_INSERT_ID();

Once again... thanks !


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-10-2007, 12:23 AM   #4
robert
 
robert's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures

np...glad you got it working.

;^)


"McHenry" <mchenry@mchenry.com> wrote in message
news:44577ba0$0$16981$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
| Thanks you for your help, greatly appreciated.
|
|
| "robert" <ab@no.spam-alama-ding-dong> wrote in message
| news:UGK5g.15$1h4.3@fe06.lga...
| >| The documentation on PHP & stored procedures is limited so can anyone
| > | confirm that the id value of the last inserted row is available via
the
| > | "insert_id" function ?
| >
| > this seems to describe it pretty well:
| >
| > ============ from php manual
| >
| > The mysqli_insert_id() function returns the ID generated by a query on a
| > table with a column having the AUTO_INCREMENT attribute. If the last
query
| > wasn't an INSERT or UPDATE statement or if the modified table does not
| > have
| > a column with the AUTO_INCREMENT attribute, this function will return
| > zero.
| > Note: Performing an INSERT or UPDATE statement using the
LAST_INSERT_ID()
| > function will also modify the value returned by the mysqli_insert_id()
| > function.
| > Return Values
| > The value of the AUTO_INCREMENT field that was updated by the previous
| > query. Returns zero if there was no previous query on the connection or
if
| > the query did not update an AUTO_INCREMENT value.
| > Note: If the number is greater than maximal int value,
mysqli_insert_id()
| > will return a string.
| >
| > ============
| >
| > now i know your great aversion to suggestion, however it is evident that
| > such could be helpful to you at this point in your learnin. as you've
| > noticed, i've cleared out my plonk list...hence this reply. if you
behave
| > and don't take offense, here's a suggestion...
| >
| > write your code generically such that you aren't strongly tied to any
| > single
| > technology. many use peardb classes to abstract the code from the db.
that
| > way, the db interface in php uses the same calls/properties/methods no
| > matter if the db is oracle, terdata, sql server, or mysql. i'm not a
huge
| > fan of peardb but i do take the same approach to abstraction. i have a
| > simplified db class that works just fine. this is the functionality of
its
| > execute() method:
| >
| > static function execute($sql, $decode = false, $returnNewId = false)
| > {
| > self::$_lastStatement = $sql;
| > $array = array();
| > $key = 0;
| > $records = mysql_query($sql);
| > $fieldCount = @mysql_num_fields($records);
| > $translation = get_html_translation_table(HTML_ENTITIES);
| > $translation = array_flip($translation);
| > while ($row = @mysql_fetch_array($records, MYSQL_NUM))
| > {
| > for ($i = 0; $i < $fieldCount; $i++)
| > {
| > $value = $row[$i];
| > if ($decode){ $value = strtr($value, $translation); }
| > $array[$key][strtoupper(@mysql_field_name($records, $i))] =
$value;
| > }
| > $key++;
| > }
| > if ($returnNewId)
| > {
| > $array = array();
| > $array[0]['ID'] = mysql_insert_id();
| > }
| > @mysql_free_result($records);
| > return $array;
| > }
| >
| > called from code, an example would look like:
| >
| > $sql = "insert into foo values('bar')";
| > $records = db::execute($sql, false, true);
| > echo '<pre>new id = ' . $records[0]['ID'] . '</pre>';
| >
| > that way, when you upgrade/change databases, you only have to alter the
db
| > class' connect and execute methods...the rest of the scripts simply call
| > the
| > same interface they always have. now, you can easily take this one step
| > further in oo design and create an actual db interface and have many of
| > the
| > db-specific classes implement that interface...then your db
upgrade/change
| > would only need to:
| >
| > $db = mysql::getInstance();
| >
| > or
| >
| > $db = sqlserver::getInstance();
| >
| > anyway...you will most likely have ignored much of what i posted beyond
| > the
| > initial php manual description of mysqli->insert_id and how it works.
| > that's
| > fine...just be civil so i don't have to start re-populating my plonk
list.
| > ;^)
| >
| >
|
| I have actually worked around the problem by having the new ID returned
form
| the procedure as a parameter using:
|
| SET _leadid=LAST_INSERT_ID();
|
| Once again... thanks !
|
|


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-10-2007, 12:23 AM   #5
McHenry
 
McHenry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures

actually didn't !

as it turns out my proposed solution didn't work:
http://bugs.mysql.com/bug.php?id=11638

So now I am unable to get a value back from a stored procedure using
parameters and the insert_id does not work either !!

Back to the drawing board...


"robert" <ab@no.spam-alama-ding-dong> wrote in message
news:Y%L5g.30$1h4.13@fe06.lga...
> np...glad you got it working.
>
> ;^)
>
>
> "McHenry" <mchenry@mchenry.com> wrote in message
> news:44577ba0$0$16981$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
> | Thanks you for your help, greatly appreciated.
> |
> |
> | "robert" <ab@no.spam-alama-ding-dong> wrote in message
> | news:UGK5g.15$1h4.3@fe06.lga...
> | >| The documentation on PHP & stored procedures is limited so can anyone
> | > | confirm that the id value of the last inserted row is available via
> the
> | > | "insert_id" function ?
> | >
> | > this seems to describe it pretty well:
> | >
> | > ============ from php manual
> | >
> | > The mysqli_insert_id() function returns the ID generated by a query on
> a
> | > table with a column having the AUTO_INCREMENT attribute. If the last
> query
> | > wasn't an INSERT or UPDATE statement or if the modified table does not
> | > have
> | > a column with the AUTO_INCREMENT attribute, this function will return
> | > zero.
> | > Note: Performing an INSERT or UPDATE statement using the
> LAST_INSERT_ID()
> | > function will also modify the value returned by the mysqli_insert_id()
> | > function.
> | > Return Values
> | > The value of the AUTO_INCREMENT field that was updated by the previous
> | > query. Returns zero if there was no previous query on the connection
> or
> if
> | > the query did not update an AUTO_INCREMENT value.
> | > Note: If the number is greater than maximal int value,
> mysqli_insert_id()
> | > will return a string.
> | >
> | > ============
> | >
> | > now i know your great aversion to suggestion, however it is evident
> that
> | > such could be helpful to you at this point in your learnin. as you've
> | > noticed, i've cleared out my plonk list...hence this reply. if you
> behave
> | > and don't take offense, here's a suggestion...
> | >
> | > write your code generically such that you aren't strongly tied to any
> | > single
> | > technology. many use peardb classes to abstract the code from the db.
> that
> | > way, the db interface in php uses the same calls/properties/methods no
> | > matter if the db is oracle, terdata, sql server, or mysql. i'm not a
> huge
> | > fan of peardb but i do take the same approach to abstraction. i have a
> | > simplified db class that works just fine. this is the functionality of
> its
> | > execute() method:
> | >
> | > static function execute($sql, $decode = false, $returnNewId = false)
> | > {
> | > self::$_lastStatement = $sql;
> | > $array = array();
> | > $key = 0;
> | > $records = mysql_query($sql);
> | > $fieldCount = @mysql_num_fields($records);
> | > $translation = get_html_translation_table(HTML_ENTITIES);
> | > $translation = array_flip($translation);
> | > while ($row = @mysql_fetch_array($records, MYSQL_NUM))
> | > {
> | > for ($i = 0; $i < $fieldCount; $i++)
> | > {
> | > $value = $row[$i];
> | > if ($decode){ $value = strtr($value, $translation); }
> | > $array[$key][strtoupper(@mysql_field_name($records, $i))] =
> $value;
> | > }
> | > $key++;
> | > }
> | > if ($returnNewId)
> | > {
> | > $array = array();
> | > $array[0]['ID'] = mysql_insert_id();
> | > }
> | > @mysql_free_result($records);
> | > return $array;
> | > }
> | >
> | > called from code, an example would look like:
> | >
> | > $sql = "insert into foo values('bar')";
> | > $records = db::execute($sql, false, true);
> | > echo '<pre>new id = ' . $records[0]['ID'] . '</pre>';
> | >
> | > that way, when you upgrade/change databases, you only have to alter
> the
> db
> | > class' connect and execute methods...the rest of the scripts simply
> call
> | > the
> | > same interface they always have. now, you can easily take this one
> step
> | > further in oo design and create an actual db interface and have many
> of
> | > the
> | > db-specific classes implement that interface...then your db
> upgrade/change
> | > would only need to:
> | >
> | > $db = mysql::getInstance();
> | >
> | > or
> | >
> | > $db = sqlserver::getInstance();
> | >
> | > anyway...you will most likely have ignored much of what i posted
> beyond
> | > the
> | > initial php manual description of mysqli->insert_id and how it works.
> | > that's
> | > fine...just be civil so i don't have to start re-populating my plonk
> list.
> | > ;^)
> | >
> | >
> |
> | I have actually worked around the problem by having the new ID returned
> form
> | the procedure as a parameter using:
> |
> | SET _leadid=LAST_INSERT_ID();
> |
> | Once again... thanks !
> |
> |
>
>



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-10-2007, 12:23 AM   #6
robert
 
robert's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures


"McHenry" <mchenry@mchenry.com> wrote in message
news:4457fad9$0$16970$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
| actually didn't !
|
| as it turns out my proposed solution didn't work:
| http://bugs.mysql.com/bug.php?id=11638
|
| So now I am unable to get a value back from a stored procedure using
| parameters and the insert_id does not work either !!
|
| Back to the drawing board...

kk

can you post the proc you're creating/using? also, post the relevant table
structures so we can prototype them on our boxes.

what version of mysql are you using? would you also post a snippet of the
code that calls the proc and handles getting the new id?

thx...we'll get it working. is this still related to parsing and storing
results from urls? if so, i may just go ahead and work up the whole thing if
you can provide some specs and urls for testing.


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-10-2007, 12:23 AM   #7
McHenry
 
McHenry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures


"robert" <ab@no.spam-alama-ding-dong> wrote in message
news:iZT5g.60$tD3.33@fe03.lga...
>
> "McHenry" <mchenry@mchenry.com> wrote in message
> news:4457fad9$0$16970$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
> | actually didn't !
> |
> | as it turns out my proposed solution didn't work:
> | http://bugs.mysql.com/bug.php?id=11638
> |
> | So now I am unable to get a value back from a stored procedure using
> | parameters and the insert_id does not work either !!
> |
> | Back to the drawing board...
>
> kk
>
> can you post the proc you're creating/using? also, post the relevant table
> structures so we can prototype them on our boxes.
>
> what version of mysql are you using? would you also post a snippet of the
> code that calls the proc and handles getting the new id?
>
> thx...we'll get it working. is this still related to parsing and storing
> results from urls? if so, i may just go ahead and work up the whole thing
> if
> you can provide some specs and urls for testing.
>
>


actually Robert, this one is totally off the famous Excel topic

I have posted the code in a post above but I didn't mention I am using PHP 5
and MySQL 5 on WindowsXP

Thanks


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-10-2007, 12:23 AM   #8
robert
 
robert's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default insert_id & Stored Procedures

| actually Robert, this one is totally off the famous Excel topic

i'm chuckling. ;^)

| I have posted the code in a post above but I didn't mention I am using PHP
5
| and MySQL 5 on WindowsXP

i assumed as much given your interface is mysqli and that you can create
stored procs...i'm using a much older version of mysql. i too am using
windows xp as a prototyping platform. i don't think the os will be too much
of a factor w/ the current issue...but who knows.


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags: , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple stored procedure error McHenry Database 1 06-10-2007 12:23 AM
Questions on MySQL 5.0.x stored procs and UDFs robocop Database 0 06-10-2007 12:19 AM
how to get records from stored procedure using ASP Bill Database 0 06-10-2007 12:19 AM
View photo stored into CD or DVD Hervé Computer Consoles 2 05-30-2007 11:08 PM


Featured Websites




All times are GMT +1. The time now is 11:47 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98