0

How to get the last insert id

INSERT INTO `cscart_static_data` ( `param_id` , `param` , `param_2` , `param_3` , 
`param_4` , `param_5` , `section` , `status` , `position` , `parent_id` , `id_path` , 
`localization` , `company_id` ) 
   VALUES ('','index.php?dispatch=postview.view', '', '', '', '1', 'A', 'A', '0', 
           '0', '152', '', '1' ); 

INSERT INTO `cscart_static_data_descriptions` ( `param_id` , `lang_code` , `descr` )
 VALUES ( SELECT LAST_INSERT_ID(), 'en', 'Blog' ); 

Here , the second query doesn't work for me, because 'SELECT LAST_INSERT_ID()' is empty .How to solve

Dharmesh Porwal
  • 1,406
  • 2
  • 12
  • 21
  • dont use the `SELECT` portion, you only need the `LAST_INSERT_ID()` part according to [MySQL :: MySQL 5.0 Reference Manual :: 20.6.14.3 How to Get the Unique ID for the Last Inserted Row](http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html) – iam-decoder Jan 06 '15 at 04:54
  • refer this http://stackoverflow.com/questions/6279579/select-last-insert-id-works-in-direct-mysql-query-but-not-intermittently-php-q – Anish Jan 06 '15 at 04:55
  • possible duplicate of [How do I get the last inserted ID of a MySQL table in PHP?](http://stackoverflow.com/questions/1685860/how-do-i-get-the-last-inserted-id-of-a-mysql-table-in-php) – Scuzzy Jan 06 '15 at 04:57
  • This old answer can help you. [http://stackoverflow.com/questions/10210273/how-to-retrieve-the-current-value-of-an-oracle-sequence-without-increment-it][1] [1]: http://stackoverflow.com/questions/10210273/how-to-retrieve-the-current-value-of-an-oracle-sequence-without-increment-it –  Jan 06 '15 at 05:01

3 Answers3

1
$sql = INSERT INTO `cscart_static_data` ( `param_id` , `param` , `param_2` , `param_3` , `param_4` , `param_5` , `section` , `status` , `position` , `parent_id` , `id_path` , `localization` , `company_id` ) VALUES ('','index.php?dispatch=postview.view', '', '', '', '1', 'A', 'A', '0', '0', '152', '', '1' ); 
 mysql_query($sql);
 $last_insert_id = mysql_insert_id();

Now use this "$last_insert_id" in your select query

Priyank
  • 3,778
  • 3
  • 29
  • 48
  • sir, I have the above code in xml file .Here how to declare the variable – Soniya Kaliappan Jan 06 '15 at 05:17
  • @SoniyaKaliappan yes you can declare php variable in xml.see how to pass php variable in xml : http://www.w3schools.com/php/php_xml_simplexml_read.asp http://stackoverflow.com/questions/15265905/php-opening-tags-and-xml-declaration – Priyank Jan 06 '15 at 05:21
0

In PHP language a function is available and that is mysqli_insert_id(). By the help of this function you can fetch last inserted id from the table.

 <!DOCTYPE html>
 <html>
<body>
    <?php
    $con = mysqli_connect('localhost', 'root', '','test');
    //$db = mysqli_select_db('test', $con);
    echo 'Date = '.date('Y-M-d H:i:s')."<br/>";
    $sql = "INSERT INTO `sample`(`name`, `address`, `date`) VALUES ('Pabitra','Kolkata'," . strtotime(date('Y-M-d H:i:s')) . ")";
    echo $sql;
    if (mysqli_query($con,$sql)) {
        echo "Insert successfully";
    }else{
        echo 'error eccured.';
    }
    ?>
    <?php
    $id = mysqli_insert_id($con);
    $sql = "select * from sample where id = ".$id;
    $result = mysqli_query($con,$sql);
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
         print_r($row);
    $myXMLData = "<?xml version='1.0' encoding='UTF-8'?>
    <note>
    <id>".$id."</id>
    <name>".$row['name']."</name>
    <address>".$row['address']."</address>
    <date>".date('Y-M-d H:i:s',strtotime($row['date']))."</date>
    </note>";

    $xml = simplexml_load_string($myXMLData) or die("Error: Cannot create object");
    print_r($xml);
    ?>

</body>

Pabitra Mondal
  • 23
  • 1
  • 11
  • sir, I have the above code in xml file .Here how to declare the variable – Soniya Kaliappan Jan 06 '15 at 05:17
  • INSERT INTO `cscart_static_data_descriptions` ( `param_id` , `lang_code` , `descr` ) VALUES ( SELECT LAST_INSERT_ID(), 'en', 'Blog' ); I have this query in xml file . here how to get SELECT LAST_INSERT_ID() ? – Soniya Kaliappan Jan 06 '15 at 06:01
0

Try this..

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

Reference:

http://php.net/manual/en/function.mysql-insert-id.php

http://php.net/manual/en/mysqli.insert-id.php

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50