Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,31 +385,27 @@ public function getValue($tableName, $column)
}

/**
* Insert method to add new row
*
* @param <string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
*
* @return boolean Boolean indicating whether the insert query was completed succesfully.
*/
public function insert($tableName, $insertData)
{
if ($this->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 <string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
*
* @return boolean Boolean indicating whether the insert query was completed succesfully.
*/
public function replace ($tableName, $insertData) {
return $this->_buildInsert ($tableName, $insertData, 'REPLACE');
}

/**
Expand Down Expand Up @@ -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 <string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
*
* @return boolean Boolean indicating whether the insert query was completed succesfully.
*/
private function _buildInsert ($tableName, $insertData, $operation)
{
if ($this->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.
Expand Down
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 ...
```

Expand Down