0

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.

0

2 Answers 2

1

Read about ORM and MVC. You factor it out by building a layered application, and encapsulating your data models in classes.

Sign up to request clarification or add additional context in comments.

Comments

0

Check out Zend Framework I have became to worship it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.