diff --git a/core/model/DB.php b/core/model/DB.php index fda6f7b54..88997cf0b 100755 --- a/core/model/DB.php +++ b/core/model/DB.php @@ -94,8 +94,8 @@ class DB { * Get the autogenerated ID from the previous INSERT query. * @return int */ - static function getGeneratedID() { - return DB::$globalConn->getGeneratedID(); + static function getGeneratedID($table) { + return DB::$globalConn->getGeneratedID($table); } /** diff --git a/core/model/Database.php b/core/model/Database.php index d58fe4522..0bb3bfacd 100755 --- a/core/model/Database.php +++ b/core/model/Database.php @@ -37,7 +37,7 @@ abstract class Database extends Object { * Get the autogenerated ID from the previous INSERT query. * @return int */ - abstract function getGeneratedID(); + abstract function getGeneratedID($table); /** * Check if the connection to the database is active. diff --git a/core/model/MySQLDatabase.php b/core/model/MySQLDatabase.php index 1921edd8e..d263a37fd 100644 --- a/core/model/MySQLDatabase.php +++ b/core/model/MySQLDatabase.php @@ -99,7 +99,7 @@ class MySQLDatabase extends Database { return new MySQLQuery($this, $handle); } - public function getGeneratedID() { + public function getGeneratedID($table) { return mysql_insert_id($this->dbConn); } diff --git a/core/model/PDODatabase.php b/core/model/PDODatabase.php index 08934e21f..46a07b8a7 100644 --- a/core/model/PDODatabase.php +++ b/core/model/PDODatabase.php @@ -153,7 +153,6 @@ class PDODatabase extends Database { $stmt = $dbConn->prepare($sql); $handle = $stmt->execute(); // Execute and save the return value (true or false) - $result = $stmt->fetchAll(); // Get the result itself if(isset($_REQUEST['showqueries'])) { $duration = microtime(true) - $starttime; @@ -164,16 +163,21 @@ class PDODatabase extends Database { $error = $stmt->errorInfo(); $this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel); } - return new PDOQuery($result); + return new PDOQuery($stmt); } /** * Get the ID for the next new record for the table. * Get the autogenerated ID from the previous INSERT query. + * Simulate mysql_insert_id by fetching the highest ID as there is no other reliable method across databases. * @return int */ - public function getGeneratedID() { - + public function getGeneratedID($table) { + $stmt = $dbConn->prepare("SELECT MAX(ID) FROM :table"); + $stmt->bindParam(":table", $table); + $handle = $stmt->execute(); + $result = $stmt->fetchColumn(); + return $handle ? $result : 0; } @@ -512,30 +516,58 @@ class PDODatabase extends Database { * A result-set from a database query (array). */ class PDOQuery extends Query { - private $result; + + /** + * The object that holds the result set. + * @var $stmt + */ + private $stmt; /** * Hook the result-set given into a Query class, suitable for use by sapphire. - * @param array $result The array of all returned values. + * @param PDO object $stmt The object of all returned values. */ - public function __construct(PDODatabase $result) { - $this->result = $result; + public function __construct(PDODatabase $stmt) { + $this->stmt = $stmt; parent::__construct(); } + + /** + * Free the result-set given into a Query class. + */ public function __destroy() { - $this->result = null; + $this->stmt = null; } + + /** + * Determine if a given element is part of the result set. + * @param string string $row The element to search for. + */ public function seek($row) { - return in_array($row, $this->result); + return in_array($row, $this->stmt->fetchAll()); } + + /** + * Return the number of results. + */ public function numRecords() { - return count($this->result); + $value = $this->stmt->fetchAll(); + return count($value); } + + /** + * + */ public function nextRecord() { - + $record = $this->stmt->fetch(PDO::FETCH_ASSOC); + if (count($record)) { + return $record; + } else { + return false; + } } }