refactoring sql in php
What is a good place to put sql commands so they do not appear in the main code, how can this be done? How can sql commands such as the following be refactorised:
function select_Query($sql, $link)
{
$query = mysqli_query($link, $sql);
if (!$query)
{
echo "Failure"; # TODO: LOG THIS
}
$data = array();
while ($row = mysqli_fetch_array($query) )
{
$data[] = $row;
}
return $data;
}
$query = select_Query("SELECT thread.title, thread.id as t_id,
thread.content
FROM thread
LIMIT $start, 30", $link);
How would you factorise the $query var part to generalise it for other sql statements - as that is fairly simple sql, however when several joins are used sql commands take up several lines in my code, I开发者_如何转开发 don't want to mix them with my php code.
Read about ORM and MVC. You factor it out by building a layered application, and encapsulating your data models in classes.
Check out Zend Framework I have became to worship it.
精彩评论