• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: mysql_insert_id() returns 0
PostPosted: Thu Feb 03, 2011 3:26 pm 
Offline
Power Poster
Power Poster

Joined: Sun Dec 28, 2008 12:15 pm
Posts: 365
Location: Roma, Italy
Hi, I'm trying to use the mysql_insert_id() php function to have the last inserted id after an INSERT STATEMENT, but the function always returns 0 (zero).

- I have a table, "MyTable", with AUTO INCREMENT ID

- I make:
$query = 'INSERT INTO '.cms_db_prefix().'MyTable (a,b) VALUES ('a-value,'b-value')';
$dbr = $db->Execute($query)

hence:
$new_generated_id = mysql_insert_id();


The new record is correctly added to "MyTable" with the new id, but $new_generated_id is always 0.

Any suggestion?

thank you


Top
 Profile  
 
 Post subject: Re: mysql_insert_id() returns 0
PostPosted: Thu Feb 03, 2011 4:33 pm 
Offline
Power Poster
Power Poster

Joined: Sun Dec 28, 2008 12:15 pm
Posts: 365
Location: Roma, Italy
Well, I tried this way and it works (but there is probably a better way...):

just after the execution of the INSERT QUERY:

Code:
$dbr = $db->Execute($query)

      $q = "SELECT LAST_INSERT_ID()";
      $q_r = $db->Execute($q);

    while ($q_r && $rowq_r = $q_r->FetchRow())
      {
      $new_generated_id = ($rowq_r['LAST_INSERT_ID()']);
      }


Top
 Profile  
 
 Post subject: Re: mysql_insert_id() returns 0
PostPosted: Thu Feb 03, 2011 4:55 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Wed Sep 05, 2007 8:03 pm
Posts: 3652
Location: The Netherlands
You should do it like this

Code:
$query = "INSERT INTO ".cms_db_prefix()."MyTable (a,b) VALUES (?,?)";
$dbr = $db->Execute($query, array($a-value,$b-value));

$insertid = $db->Insert_ID();

_________________
Get nice guestbook messages from your site visitors with the brand new Gbook module.
Integrate Piwik Web Analytics in your site admin with the Piwik module.
Extend your global site settings with the Custom Global Settings module.
The Fourth Dutch CMS Made Simple Workshop on April 9 2011 was great fun! Read all about it here
Announcement: The Fifth Dutch CMS Made Simple Workshop coming up?


Top
 Profile  
 
 Post subject: Re: mysql_insert_id() returns 0
PostPosted: Thu Feb 03, 2011 5:24 pm 
Offline
Power Poster
Power Poster

Joined: Sun Dec 28, 2008 12:15 pm
Posts: 365
Location: Roma, Italy
Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
A2 Hosting