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

  • SEARCH
  • Home
  • 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 8700467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:10:32+00:00 2026-06-13T02:10:32+00:00

I have an insert query, and I want to get the ID from the

  • 0

I have an insert query, and I want to get the ID from the table. I have been searching, and I found lastInsertId() for PDO. When I want to use it, I get PHP errors.

This is my code:

$db = new database();
$naam = $db->quoteQuery($_POST['naam']);
$barcode = $db->quoteQuery($_POST['barcode']);
$sql = "INSERT INTO products(name, barcode) VALUES (".$name.",".$barcode.")";
$results = $db->executeQuery($sql);
$lastid = $results->lastInsertId();

But this gives an error, this one:

Fatal error: Call to undefined method PDOStatement::lastInsertId() in /home/onlineweuh/domains/onlinewebapps.nl/public_html/vsb/admin/add-product.class.php on line 297

My database class:

    class database 
{
    private $handleDB;
    public function __construct()
    {
        $host = ;
        $user = ;
        $database = ;
        $password = ;
        try
        {
            $this->handleDB = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
        }
        catch (PDOException $e)
        {
            print_r($e);
        }

        $this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    }

I hope someone can help me solve it, I want the ID which is given at the insert Query.

  • 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-06-13T02:10:33+00:00Added an answer on June 13, 2026 at 2:10 am

    You get the lastinsertid from the PDO object and not your results object.

    Try $db->lastInsertId()

    edit below.

    Your database class is encapsulating your handleDB / PDO object. Since the handleDB variable is private, you cannot access this outside your class. You would need to either make it public like so;

    class database 
    {
        public $handleDB;
        public function __construct()
        {
            $host = 'removed';
            $user = 'removed';
            $database = 'removed';
            $password = 'removed';
            try
            {
                $this->handleDB = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
            }
            catch (PDOException $e)
            {
                print_r($e);
            }
    
            $this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        }
    
    }
    

    Now you can call $db->handleDB->lastInsertId();

    Or you could expose the handleDB->lastInsertId() as a function like:

    class database 
    {
        private $handleDB;
        public function __construct()
        {
            $host = 'remove';
            $user = 'removed';
            $database = 'removed';
            $password = 'removed';
            try
            {
                $this->handleDB = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
            }
            catch (PDOException $e)
            {
                print_r($e);
            }
    
            $this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        }
    
        public function lastInsertId(){
            return $this->handleDB->lastInsertId();
        }
    
    }
    

    You would call using $db->lastInsertId();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below sql query to insert the records from a table. The
I have an INSERT query that is pulling data from two tables and inserting
I have a query: insert into #tmpRcIzm select distinct v.id_art, 'Y' from openxml(@hDoc, '/art_komp/nol')
I have the following query: INSERT INTO table (a, b, c) VALUES (NOW(), NOW()
I am using the following query to get the transactions from a table made
I have a problem regarding this query. I want to get the applicant latest
I'm trying to pull results from a table that have been inserted today. Each
I have two tables users registered_members I want to confirm values from user table
I can get data from MySql with use of php using android successfully. But
I'm having a bit of trouble with an INSERT query. I have a table

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.