-2

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;
4

1 回答 1

0

insert()函数中,你从来没有设置过$lastid,所以它永远不会有正确的值。你需要这样的东西:

insert()函数中更改这一行:

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

对此:

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

lastInsertId()即使没有上述修改,您也应该能够从类外部访问,如下所示:

$db->insert("users",$insert);
echo $db->lastInsertId();
于 2012-06-22T12:31:48.207 回答