Пример #1
0
function mysql_create_select_statement($table, $fields = "*", $select_addons = array())
{
    $fields = !$fields ? "*" : $fields;
    $sql_statement = "SELECT ";
    if (is_array($select_addons) and (isset($select_addons['DISTINCT']) or isset($select_addons['UNIQUE']))) {
        $sql_statement .= "DISTINCT ";
    }
    if (is_array($select_addons) and (isset($select_addons['FOUND_ROWS']) or isset($select_addons['SQL_CALC_FOUND_ROWS']))) {
        $sql_statement .= "SQL_CALC_FOUND_ROWS ";
    }
    if (is_array($fields)) {
        $sql_statement .= join(", ", $fields) . " FROM {$table}";
    } else {
        $sql_statement .= "{$fields} FROM {$table}";
    }
    if (!is_array($select_addons)) {
        # incase this is sent as a text string SQL statement addon
        $sql_statement .= " " . trim($select_addons);
    } else {
        # select structure - SELECT FROM JOIN WHERE GROUP HAVING ORDER LIMIT
        #  http://dev.mysql.com/doc/refman/5.0/en/select.html
        if (is_array($select_addons['JOIN'])) {
            if (count($select_addons['JOIN'])) {
                # the array should be formated as ['JOIN_TYPE'], ['TABLE'], ['ON'] OR ['USING']
                # http://dev.mysql.com/doc/refman/5.0/en/join.html
                # http://en.wikipedia.org/wiki/Join_(SQL)
                # http://www.tizag.com/sqlTutorial/sqljoin.php
                #  table JOIN table1 ON ( table.field = table1.field )
                # USING links columns of the name in the two tables instead of having table1.id = table2.id
                #  table JOIN table1 USING ( id )
                # NATURAL JOIN automatically links up corresponding field names
                # alternative join means - SELECT table1.id, table2.name from table1,table2 where table1.id = table2.id
                print "Need to make this [mysql_create_select_statement] JOIN";
                exit;
            }
        } else {
            if ($select_addons['JOIN']) {
                # LEFT JOIN t2 ON (t1.a = t2.a) / LEFT JOIN t2 USING (a)
                $sql_statement .= " " . $select_addons['JOIN'];
            }
        }
        if (is_array($select_addons['WHERE'])) {
            $sql_statement .= mysql_build_where_clause($select_addons['WHERE']);
        } else {
            if ($select_addons['WHERE']) {
                $sql_statement .= " WHERE " . $select_addons['WHERE'];
            } else {
                if (is_array($select_addons['where'])) {
                    $sql_statement .= mysql_build_where_clause($select_addons['where']);
                } else {
                    if ($select_addons['where']) {
                        $sql_statement .= " WHERE " . $select_addons['where'];
                    }
                }
            }
        }
        if ($select_addons['GROUP']) {
            $sql_statement .= " GROUP BY " . $select_addons['GROUP'];
        } else {
            if ($select_addons['GROUP BY']) {
                $sql_statement .= " GROUP BY " . $select_addons['GROUP BY'];
            }
        }
        if ($select_addons['HAVING']) {
            $sql_statement .= " HAVING " . $select_addons['HAVING'];
        }
        if ($select_addons['ORDER']) {
            $sql_statement .= " ORDER BY " . $select_addons['ORDER'];
        } else {
            if ($select_addons['ORDER BY']) {
                $sql_statement .= " ORDER BY " . $select_addons['ORDER BY'];
            } else {
                if ($select_addons['SORT']) {
                    $sql_statement .= " ORDER BY " . $select_addons['SORT'];
                } else {
                    if ($select_addons['SORT BY']) {
                        $sql_statement .= " ORDER BY " . $select_addons['SORT BY'];
                    }
                }
            }
        }
        if ($select_addons['LIMIT']) {
            $sql_statement .= " LIMIT " . $select_addons['LIMIT'];
        }
    }
    return $sql_statement;
}
Пример #2
0
function mysql_increment_field($connect_id, $table, $field, $where_fields, $by = 1)
{
    if (!$connect_id) {
        return;
    }
    $sql_statement = "UPDATE {$table} SET {$field}={$field}+{$by}";
    if (is_array($where_fields)) {
        $sql_statement .= mysql_build_where_clause($where_fields);
    } else {
        if ($where_fields) {
            $sql_statement .= " WHERE " . $where_fields;
        }
    }
    if ($GLOBALS['sql_debug']) {
        print $sql_statement . "<br>\r\n";
    }
    $sql_result = mysql_query($sql_statement, $connect_id);
}