?
Solved

fetch row information

Posted on 2011-05-01
4
Medium Priority
?
442 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:Neil_Bradley
  • 3
4 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35502767
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");

?>
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 35502771
I'm sorry: this is wrong. Do simply

<?php
 $conn = db_connect();
  $result = $conn->query("insert into myTable values
                         ('', '".$date."','".$_SESSION['trading']."','".$_SESSION['title']."')");
  printf ("Your reference number is %d.\n", $conn->insert_id);
?>

About this youcan learn more here: http://php.net/manual/en/mysqli.insert-id.php

Cheers
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 35502793
Perfect, that has got it!
Nice tidy solution..
Cheers,
Neil
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35502799
Glad to help you. Good luck with your project.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question