2012-06-22 73 views
-2

我已經從Google代碼之一下載了follwing代碼,但問題在於獲取上次插入ID。無法獲取上一個插入ID

請參閱run()功能

用於運行不能被包含的刪除,插入,選擇,或更新的方法來處理自由形式的SQL語句,這種方法。如果未產生SQL錯誤,則此方法將返回DELETE,INSERT和UPDATE語句的受影響行數,或SELECT,DESCRIBE和PRAGMA語句的關聯結果數組。我使用插入記錄

$insert = array(
     "userid" => 12345, 
     "first_name" => "Bhavik", 
     "last_name" => "Patel", 
     "email" => "[email protected]", 
     "password" => "202cb962ac59075b964b07152d234b70" 

    $db->insert("users",$insert); 
    echo $db->lastid; 
+1

'implode(「|」,array(「刪除「,」插入「,」更新「))'哦,上帝請爲什麼? – lanzz

回答

0

insert()功能

<?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; 
      } 
     } 




    } 

    ?> 

代碼,你從來沒有設置$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(); 
+0

什麼是$行? –

+0

這是從'$ pdostmt-> rowCount();'返回的計數。 – nickb

+0

$ db-> lastInsertId();總是在我的代碼中返回0 –