2006-09-04, 06:46 AM
so ive got some old db queries that look like this:
but how do i convert these to the new versions?
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."shouts ORDER BY dateline LIMIT $start, 5");
simple selects i thinkbut how do i convert these to the new versions?
/**
* Performs a simple select query.
*
* @param string The table name to be queried.
* @param string Comma delimetered list of fields to be selected.
* @param string SQL formatted list of conditions to be matched.
* @param array List of options, order by, order direction, limit, limit start
*/
function simple_select($table, $fields="*", $conditions="", $options=array())
{
$query = "SELECT ".$fields." FROM ".$table;
if($conditions != "")
{
$query .= " WHERE ".$conditions;
}
if(isset($options['order_by']))
{
$query .= " ORDER BY ".$options['order_by'];
if(isset($options['order_dir']))
{
$query .= " ".strtoupper($options['order_dir']);
}
}
if(isset($options['limit_start']) && isset($options['limit']))
{
$query .= " LIMIT ".$options['limit_start'].", ".$options['limit'];
}
elseif(isset($options['limit']))
{
$query .= " LIMIT ".$options['limit'];
}
return $this->query($query);
}
/**
* Performs a select query
*
* @param array Array of query information
* @return resource The query data.
*/
function select_query($data)
{
$select = array($data['select']);
$join_sql = '';
if(is_array($data['joins']))
{
foreach($data['joins'] as $join)
{
if($join['select'])
{
$select[] = $join['select'];
}
if($join['type'] == "left")
{
$join_sql .= "LEFT JOIN {$join['table']} ";
if($join['where'])
{
$join_sql .= "ON ({$join['where']}) ";
}
}
else if($join['type'] == "inner")
{
$join_sql .= "INNER JOIN {$join['table']} ";
if($join['where'])
{
$join_sql .= "ON ({$join['where']}) ";
}
}
}
}
$select = implode(", ", $select);
$rows = "";
if($data['rows'] != "")
{
$rows = "({$data['rows']})";
}
$query = "SELECT {$select} FROM {$data['table']} $rows {$join_sql}";
if($data['where'])
{
$query .= " WHERE {$data['where']}";
}
if($data['order'])
{
$query .= " ORDER BY {$data['order']}";
}
if(is_array($data['limit']))
{
$query .= " LIMIT {$data['limit'][0]}";
if($data['limit'][1])
{
$query .= ", {$data['limit'][1]}";
}
}
return $this->query($query);
}
/**
* Build an insert query from an array.
*
* @param string The table name to perform the query on.
* @param array An array of fields and their values.
* @return resource The query data.
*/
function insert_query($table, $array)
{
$comma = $query1 = $query2 = "";
if(!is_array($array))
{
return false;
}
$comma = "";
$query1 = "";
$query2 = "";
foreach($array as $field => $value)
{
$query1 .= $comma.$field;
$query2 .= $comma."'".$value."'";
$comma = ", ";
}
return $this->query("
INSERT
INTO ".$table." (".$query1.")
VALUES (".$query2.")
");
}