CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • how to get mysql insert id > request some pointers , thanks
Pages: [1]

Author Topic: how to get mysql insert id > request some pointers , thanks  (Read 2571 times)

saffrongeek

  • Guest
how to get mysql insert id > request some pointers , thanks
September 22, 2009, 06:08:52 am
Hi ,

      I was able to insert in to address table using CRM_Core_DAO::executeQRY($insrtSql); but I am not able to figure out how wud I get the insert id for this record. Is there any function equivalent to mysql_insert_id() in civicrm.

      Thanks in advance, I remain

With regards,

saffron

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: how to get mysql insert id > request some pointers , thanks
September 22, 2009, 06:55:33 am

Code: [Select]
$dao = CRM_Core_DAO::executeQuery($insrtSql);
echo "{$dao->id} should be the value of the inserted id"

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

saffrongeek

  • Guest
Re: how to get mysql insert id > request some pointers , thanks
September 22, 2009, 10:42:58 am
Hi Lobo,

             Thanks again.

Regards,

saffron

saffrongeek

  • Guest
Re: how to get mysql insert id > request some pointers , thanks
September 22, 2009, 05:02:00 pm
Hi Lobo,
               
                 I dont know why I am not getting the mysql insert id of previous insert. My code looks as below:-
        
Code: [Select]
       $resultEntity = CRM_Core_DAO::executeQuery( $insrtQry );
         $loc_Address_id = $resultEntity->id;
         echo 'Insert id'.$loc_Address_id; die;
               $resultEntity->free();

               Am i missing anything? :(

Regards,

Harish

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: how to get mysql insert id > request some pointers , thanks
September 22, 2009, 09:36:09 pm

argh

yes that method does not work :(

We rarely do insert/update queries via sql (we use DB_DataObject) which does all the work and we get the next ID as part of the return set

you'll have to use the php/mysql function (mysql_next_id??) to retrieve it given a db cnnection etc. I dont know the exact details on how u'd go about doing it

sorry

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Deepak Srivastava

  • Ask me questions
  • ****
  • Posts: 677
  • Karma: 65
Re: how to get mysql insert id > request some pointers , thanks
September 23, 2009, 12:03:51 am
LAST_INSERT_ID() in MySQL gives you the Value of the AUTOINCREMENT column for the last INSERT.

In CiviCRM you can make it work this way -
Code: [Select]
$resultEntity = CRM_Core_DAO::executeQuery( $insrtQry );
// ... anything you want to do here with $resultEntity
$lastId = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
echo "last id= $lastId";
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

saffrongeek

  • Guest
Re: how to get mysql insert id > request some pointers , thanks
September 23, 2009, 05:41:38 am
Hi Deepak,

                  Thanks. It worked.

                   Why I was trying was , my below code was populating event , loc_block, and address tables but after creation i do not find the event record to have the loc_block id in the loc_block field. I cudnt find more to read the structure of location params structure but the code below was doing it for me though earlier mentioned issue exists.
Code: [Select]

$address = array(
"location" => array(
"1" => array(
"location_type_id" => 3,
"is_primary" => 1,
"address" => array(
"street_address" => isset($rownohbm['course_location'])?$rownohbm['course_location']:'',
"supplemental_address_1" => isset($rownohbm['course_address_1'])?$rownohbm['course_address_1']:"",
"supplemental_address_2" => isset($rownohbm['course_address_2'])?$rownohbm['course_address_2']:"",
"city" => isset($rownohbm['course_city'])? $rownohbm['course_city']:'',
"postal_code" => isset($nationalSet['zip_code'])? $nationalSet['zip_code']:$nationalSet['zip_code2'],
"postal_code_suffix" => NULL,
"state_province_id" => isset($rownohbm['hbm_state_id'])? (int)$rownohbm['hbm_state_id']:(int)$rownohbm['hbm_state_id'],
"geo_code_1" => NULL,
"geo_code_2" => NULL
),
// "phone" => $phones,
// "email" => $email
          )
          )
          );
          // $location["location_option"] = $crmParams['location_option'];
          $location["entity_id"]=$crm_event_id;
          $location["entity_table"]='civicrm_event';
          $location = $location + $address;
          unset($address);
          var_dump($location['location']);
require_once "CRM/Core/BAO/Location.php";
$newLocation = CRM_Core_BAO_Location::create($location, true, 'event');
die;         

                   Since this was not working , I tried to manually populate the address, then location and then using the info event table. That was when i found this issue of last insert id as a blockade.

Thanks once again,

Harish

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • how to get mysql insert id > request some pointers , thanks

This forum was archived on 2017-11-26.