MySQL::AutoInsertUpdate PHP Method

AutoInsertUpdate() public method

Automatically does an INSERT or UPDATE depending if an existing record exists in a table
public AutoInsertUpdate ( string $tableName, array $valuesArray, array $whereArray ) : boolean
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$whereArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect).
return boolean Returns TRUE on success or FALSE on error
    public function AutoInsertUpdate($tableName, $valuesArray, $whereArray)
    {
        $this->ResetError();
        $this->SelectRows($tableName, $whereArray);
        if (!$this->Error()) {
            if ($this->HasRecords()) {
                return $this->UpdateRows($tableName, $valuesArray, $whereArray);
            } else {
                return $this->InsertRow($tableName, $valuesArray);
            }
        } else {
            return false;
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Save one or more preferences for a user. If no user ID is given the current user is used.
  * The array has to assign every preference key a value to store.
  * Example: array ( 'setting1' => 'value1', 'setting2' => 'value2');
  *
  * A prefix can be specified, which will be prepended to every preference key.
  *
  * @param array   $data   key/value pairs to store
  * @param string  $prefix prefix for all preferences
  * @param integer $userId (optional) id of another user than the current
  * @return boolean        true on success, false on failure
  * @author sl
  */
 public function user_set_preferences(array $data, $prefix = '', $userId = null)
 {
     if ($userId === null) {
         $userId = $this->kga['user']['userID'];
     }
     if (!$this->conn->TransactionBegin()) {
         $this->logLastError('user_set_preferences');
         return false;
     }
     $table = $this->kga['server_prefix'] . "preferences";
     $filter['userID'] = MySQL::SQLValue($userId, MySQL::SQLVALUE_NUMBER);
     $values['userID'] = $filter['userID'];
     foreach ($data as $key => $value) {
         $values['option'] = MySQL::SQLValue($prefix . $key);
         $values['value'] = MySQL::SQLValue($value);
         $filter['option'] = $values['option'];
         $this->conn->AutoInsertUpdate($table, $values, $filter);
     }
     return $this->conn->TransactionEnd();
 }
All Usage Examples Of MySQL::AutoInsertUpdate