PHP/SQL retrieve newly inserted row

I am inserting the following data into an SQL table using PHP.

    $query1 = 'INSERT INTO log_index_level_1 (LOG_INDEX, LOT_ID, WAFER_ID, MAP_REV, PROBE_DATE) VALUES ("", "' . $LOT_ID . '", "' . $WAFER_ID . '", "' . $MAP_REV. '", ' . "STR_TO_DATE('" . $PROBE_DATE . "', '%m-%d-%Y'" . '))';

    $result1 = mysql_query($query1) 

    echo mysql_result($result1, 0) . "\n;

How do I retrieve the first row of the data I just inserted? i.e. I want to echo LOG_INDEX.

The above code does not work, since $result1 returns 1 and not the actual DATA itself.

Answers


I have not tested the code below .

<?php

  /*
  ........... your connection code ... etc
  .............
  */

  $query1 = 'INSERT INTO log_index_level_1 (LOG_INDEX, LOT_ID, WAFER_ID, MAP_REV, PROBE_DATE) VALUES ("", "' . $LOT_ID . '", "' . $WAFER_ID . '", "' . $MAP_REV. '", ' . "STR_TO_DATE('" . $PROBE_DATE . "', '%m-%d-%Y'" . '))';

  $result1 = mysql_query($query1);

  $query2 = 'select last_insert_id() as lastId';

  $result2 = mysql_query($query2);

  if ($row = mysql_fetch_object($result2)) {

    $lastId = $row->lastId;

    $query3 = "select * from log_index_level_1 aa where aa.LOG_INDEX = ".$lastId;

    $result3 = mysql_query($query3);

    if ($lastRow = mysql_fetch_object($result3)) {
      // do something with $lastRow
      // $lastRow->LOT_ID , $lastRow->WAFER_ID ... etc
    }

    mysql_free_result($result3);
  }

  mysql_free_result($result2);
  mysql_free_result($result1);
?>

Thanks for your inspirations.

Here's the code I looked up that solved it.

$result_liq = mysql_query("SHOW TABLE STATUS LIKE 'log_index_level_1'");
$row = mysql_fetch_array($result_liq);
$LOG_INDEX = $row['Auto_increment'];

Need Your Help

ASP.NET : Possible to create a UserControl that include inside markup content?

c# asp.net user-controls

I'm trying to achieve something google isn't able to give answer. I'm trying to create an asp.net usercontrol that when I put content into it's open close tag will include it for me to be still abl...

Integration of adMob: Unexpectedly found nil while unwrapping an optional value

swift sprite-kit admob

I have switched over from iAd to adMob and am having a hard time integrating the banner ads.