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 8115405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:28:34+00:00 2026-06-06T03:28:34+00:00

I have downloaded follwing code from one of Google Codes but problem is in

  • 0

I have downloaded follwing code from one of Google Codes but problem is in getting Last Insert ID.

Please see in run() function

This method is used to run free-form SQL statements that can’t be handled by the included delete, insert, select, or update methods. If no SQL errors are produced, this method will return the number of affected rows for DELETE, INSERT, and UPDATE statements, or an associate array of results for SELECT, DESCRIBE, and PRAGMA statements.

<?php

    class db extends PDO {

        private $error;
        private $sql;
        private $bind;
        private $errorCallbackFunction;
        private $errorMsgFormat;
        public $lastid;
        public function __construct($dsn, $user = "", $passwd = "") {
            $options = array(
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            );

            try {
                parent::__construct($dsn, $user, $passwd, $options);
            } catch (PDOException $e) {
                $this->error = $e->getMessage();
            }
        }





        private function filter($table, $info) {
            $driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
            if ($driver == 'sqlite') {
                $sql = "PRAGMA table_info('" . $table . "');";
                $key = "name";
            } elseif ($driver == 'mysql') {
                $sql = "DESCRIBE " . $table . ";";
                $key = "Field";
            } else {
                $sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $table . "';";
                $key = "column_name";
            }

            if (false !== ($list = $this->run($sql))) {
                $fields = array();
                foreach ($list as $record)
                    $fields[] = $record[$key];
                return array_values(array_intersect($fields, array_keys($info)));
            }
            return array();
        }

        private function cleanup($bind) {
            if (!is_array($bind)) {
                if (!empty($bind))
                    $bind = array($bind);
                else
                    $bind = array();
            }
            return $bind;
        }

        public function insert($table, $info) {
            $fields = $this->filter($table, $info);
            $sql = "INSERT INTO " . $table . " (" . implode($fields, ", ") . ") VALUES (:" . implode($fields, ", :") . ");";
            $bind = array();
            foreach ($fields as $field)
                $bind[":$field"] = $info[$field];
            return $this->run($sql, $bind);
        }

        public function run($sql, $bind = "") {
            $this->sql = trim($sql);
            $this->bind = $this->cleanup($bind);
            $this->error = "";

            try {
                $pdostmt = $this->prepare($this->sql);
                if ($pdostmt->execute($this->bind) !== false) {
                    if (preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql))
                        return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
                    elseif (preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql)) {
                       echo $this->lastInsertId();
                        return $pdostmt->rowCount();
                    }
                }
            } catch (PDOException $e) {
                $this->error = $e->getMessage();
                return false;
            }
        }




    }

    ?>

Code i use to Insert Record

    $insert = array(
        "userid" => 12345,
        "first_name" => "Bhavik",
        "last_name" => "Patel",
        "email" => "bhavik@sjmtechs.com",
        "password" => "202cb962ac59075b964b07152d234b70"

   $db->insert("users",$insert);
    echo $db->lastid;
  • 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-06T03:28:35+00:00Added an answer on June 6, 2026 at 3:28 am

    In the insert() function, you’ve never set $lastid, so it never has the correct value. You’ll need something like this:

    Change this line in the insert() function:

    return $this->run($sql, $bind);
    

    To this:

    $rows = $this->run($sql, $bind);
    $this->lastid = $this->lastInsertId();
    return $rows;
    

    You should also be able to access the lastInsertId() from outside of the class, even without the above modifications, like so:

    $db->insert("users",$insert);
    echo $db->lastInsertId();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

all I have downloaded source code from google android website following google's guide. My
I have downloaded sample code from Apple Center.I also have gone through following question:
I have downloaded a web project from CodePlex and am getting the following error
(Problem solved, please see the updates) I have some files that have incorrect filenames
I have downloaded Skype4COM from Skype so my question is, can i integrate it
I have downloaded cscope 15.6 on MacOSX. But when i try launching it in
I have used an open source code from CodeProject to read email from incoming
I have installed NHibernate for my ASP.NET web app although I'm getting a problem
What I have now is the following code: Tutorial tutorial = (from tutorial in
I have been trying to follow this example (download the source code from 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.