Link to home
Start Free TrialLog in
Avatar of Neil_Bradley
Neil_BradleyFlag for New Zealand

asked on

fetch row information

I have a simple upload script. The primary key "id_ref" has an "auto increment" property.
What I would like to do is insert the new row then immediately retrieve the id_ref from that new row so it can be echoed on the page.. IE Your information has been uploaded. Your reference number is "echo the id_ref".
Help appreciated.
N

<?php

function db_connect() {
   $result = new mysqli('localhost', 'demo', 'password', 'demo');
   if (!$result) {
     throw new Exception('Could not connect to database server');
   } else {
     return $result;
   }
}

?>


<?php
 $conn = db_connect();
  $result = $conn->query("insert into myTable values
                         ('', '".$date."','".$_SESSION['trading']."','".$_SESSION['title']."')");

?>

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Use insert_id function to perform a query and select the last inserted record, then get the value

<?php
  $result = $conn->query("selec id_ref from myTable where id_ref = $conn->insert_id");

?>
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Neil_Bradley

ASKER

Perfect, that has got it!
Nice tidy solution..
Cheers,
Neil
Glad to help you. Good luck with your project.