Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:59:37+00:00 2026-05-31T04:59:37+00:00

Supposing I am inserting a row into a table in MySql by php code,

  • 0

Supposing I am inserting a row into a table in MySql by php code, and my table has an ID which is not given but it is auto_increment – how can I retrieve the ID of the row I just inserted?
My query in php looks like this:

$query = "insert into " . $this->tabel . " values (false,";

And after this I am filling the query with the values given as parameters in my function.
However, I need to update an internal variable of my php class with the current ID. How can I obtain it?

Thank you

public function insert ($keyValueSet) {

    $query = "insert into " . $this->tabel . " values (false,";
    $connection = new mysqli(ADDRESS, USERNAME, PASSWORD, DB);
    $cols = $this->getTableDesc();
    // $query construction ......
    $result = $connection->query($query);
    echo "ID: " . mysql_insert_id ();
    if ($result != 1)
        echo "Exception";
    $connection->close();
}

The incremented field is the first field (where i say: values (false,) and MySql does auto-increment this field, as I saw in the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:59:38+00:00Added an answer on May 31, 2026 at 4:59 am

    For the default mysql_connect()

    $id = mysql_insert_id();
    

    For procedural MySQLi e.g mysqli_connect()

    $id = mysqli_insert_id();
    

    For Object Oriented MySQLi e.g. $db = new mysqli()

    $id = $db->insertid; // replace $db with your DB variable name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem about inserting values into the table with sqlite. supposing the
Supposing I have the following code which returns a Javascript object which I can
Supposing my table has 10 columns .... which columns i have to choose to
Suppose I am inserting a string into a table as follows: table.insert(tbl, mystring) and
Supposing I have one perl in /usr/bin (which came along with my os-distribution) and
Supposing I have this: foo bar 1 and foo bar 2 How can I
My program has been working perfectly so far, but it turns out that I've
Supposing I have a set of comma separated lists which I need to display
Supposing I have the following script <div id=something> <?php // execute a loop ?>
Below is the code example for the insert code of a node into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.