SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

SQL
Viver é algo que se faz agora ou nunca...
Qual você faz?
Piet Hein
< INSERT | SELECT | UPDATE >

SELECT

  • Selecting specific columns:
    SELECT
    col1[,col2,...,colN]
    FROM
    table
    WHERE
    Column1='value1' [AND Column2='value2'...]
    [ORDER BY Cols];
  • Selecting all columns:
    SELECT
    *
    FROM
    table
    WHERE
    Column1='value1' [AND Column2='value2'...]
    [ORDER BY Cols];
PHP Listing: Select.php.
    //*
    //* Perform a select query on Table $table in the current DB.
    //* Returns each match as a hash of the field names in
    //* in $fields or all data if $fields is not an array.

    function Sql_Select_Hashes($where="",$fieldnames=array(),$orderby="",$postprocess=FALSE,$table="",$limit="",$offset="")
    {
        if (!$this->Sql_Table_Exists($table)) { return array(); }

        $this->LastSqlWhere=
            $this->Sql_Select_Hashes_Query
            (
                $where,
                $fieldnames,
                $orderby,
                $table,
                $limit,
                $offset
            );

        $result=$this->DB_Query_2Assoc_List($this->LastSqlWhere);

        if ($result && $postprocess && method_exists($this,"PostProcessItemList"))
        {
            $this->PostProcessItemList($result);
        }

        if (empty($result)) { $result=array(); }
        
        return $result;
    }


    //*
    //* Generates the SQL slect hashes query.
    //* 

    function Sql_Select_Hashes_Query($where="",$fields=array(),$orderby="",$table="",$limit="",$offset="")
    {
        if (empty($table)) { $table=$this->SqlTableName(); }
        
        if (is_array($where))
        {
            $where=$this->Hash2SqlWhere($where);
        }

        if (empty($fields) || $fields=="*")
        {
            $fieldnames=array();
        }
        else
        {
            $fieldnames=$this->Sql_Table_Fields_Exists($fields,$table);

            if (!preg_grep('/^ID$/',$fieldnames))
            {
                array_push($fieldnames,"ID");
            }
        }
       

        $query=
            'SELECT '.
            $this->Sql_Table_Column_Names_Qualify($fieldnames).
            ' FROM '.
            /* $dbstring. */
            $this->Sql_Table_Name_Qualify_With_DB($table).
            '';
        
        if (preg_match('/\S/',$where))
        {
            $query.=' WHERE '.$where;
        }
        
        if (!empty($orderby))
        {
            $query.=
                ' ORDER BY '.
                $this->Sql_Table_Column_Sort_Names_Qualify($orderby);
        }

        if (!empty($limit))
        {
            $query.=' LIMIT '.$limit;
        }
        if (!empty($offset))
        {
            $query.=' OFFSET '.$offset;
        }

        return $query;
    }
< INSERT | SELECT | UPDATE >
Messages:
0 secs.