diff --git a/MysqliDb.php b/MysqliDb.php index dddd9612..d6f275b0 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -385,31 +385,27 @@ public function getValue($tableName, $column) } /** + * Insert method to add new row * * @param isSubQuery) - return; - - $this->_query = "INSERT INTO " .self::$prefix . $tableName; - $stmt = $this->_buildQuery(null, $insertData); - $stmt->execute(); - $this->_stmtError = $stmt->error; - $this->reset(); - $this->count = $stmt->affected_rows; - - if ($stmt->affected_rows < 1) - return false; - - if ($stmt->insert_id > 0) - return $stmt->insert_id; + public function insert ($tableName, $insertData) { + return $this->_buildInsert ($tableName, $insertData, 'INSERT'); + } - return true; + /** + * Replace method to add new row + * + * @param _buildInsert ($tableName, $insertData, 'REPLACE'); } /** @@ -697,6 +693,35 @@ protected function _buildPair ($operator, $value) { return " " . $operator . " (" . $subQuery['query'] . ") " . $subQuery['alias']; } + /** + * Internal function to build and execute INSERT/REPLACE calls + * + * @param isSubQuery) + return; + + $this->_query = $operation . " " . implode (' ', $this->_queryOptions) ." INTO " .self::$prefix . $tableName; + $stmt = $this->_buildQuery (null, $insertData); + $stmt->execute(); + $this->_stmtError = $stmt->error; + $this->reset(); + $this->count = $stmt->affected_rows; + + if ($stmt->affected_rows < 1) + return false; + + if ($stmt->insert_id > 0) + return $stmt->insert_id; + + return true; + } + /** * Abstraction method that will compile the WHERE statement, * any passed update data, and the desired rows. diff --git a/readme.md b/readme.md index 006100c5..ad93315a 100644 --- a/readme.md +++ b/readme.md @@ -107,9 +107,11 @@ if ($id) echo 'user was created. Id=' . $id; else echo 'insert failed: ' . $db->getLastError(); - ``` +### Replace Query +replace() method implements same API as insert(); + ### Update Query ```php $data = Array ( @@ -294,17 +296,17 @@ echo "Showing {$count} from {$db->totalCount}"; ``` ### Query Keywords -To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of mysql keywords to INSERT , SELECT , UPDATE, DELETE query: +To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of the mysql keywords to INSERT (), REPLACE (), GET (), UPDATE (), DELETE() method: ```php $db->setQueryOption('LOW_PRIORITY'); -$db->insert($table,$param); +$db->insert ($table, $param); // GIVES: INSERT LOW_PRIORITY INTO table ... ``` Also you can use an array of keywords: ```php $db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE')); -$db->insert($table,$param); +$db->insert ($table,$param); // GIVES: INSERT LOW_PRIORITY IGNORE INTO table ... ```