MySQL::BuildSQLUpdate PHP Method

BuildSQLUpdate() public static method

[STATIC] Builds a SQL UPDATE statement
public static BuildSQLUpdate ( string $tableName, array $valuesArray, array $whereArray = null ) : string
$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 (Optional) 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). If not specified then all values in the table are updated.
return string Returns a SQL UPDATE statement
    public static function BuildSQLUpdate($tableName, $valuesArray, $whereArray = null)
    {
        $sql = "";
        foreach ($valuesArray as $key => $value) {
            if (strlen($sql) == 0) {
                $sql = "`" . $key . "` = " . $value;
            } else {
                $sql .= ", `" . $key . "` = " . $value;
            }
        }
        $sql = "UPDATE `" . $tableName . "` SET " . $sql;
        if (is_array($whereArray)) {
            $sql .= self::BuildSQLWhereClause($whereArray);
        }
        return $sql;
    }

Usage Example

Esempio n. 1
0
/**
 * set cleared state for exp entry
 *
 * @param integer $id -> ID of record
 * @param boolean $cleared -> true if record is cleared, otherwise false
 * @global array  $kga kimai-global-array
 * @author sl
 */
function export_expense_set_cleared($id, $cleared)
{
    global $kga, $database;
    $conn = $database->getConnectionHandler();
    $table = $kga['server_prefix'] . "expenses";
    $values['cleared'] = $cleared ? 1 : 0;
    $filter['expenseID'] = MySQL::SQLValue($id, MySQL::SQLVALUE_NUMBER);
    $query = MySQL::BuildSQLUpdate($table, $values, $filter);
    if ($conn->Query($query)) {
        return true;
    } else {
        return false;
    }
}
All Usage Examples Of MySQL::BuildSQLUpdate