Fetching and formatting content from a database I've wrote this script to fetch and format content from my DB. It also counts how many result there are and separates them into pages. I'm barely learning PHP and MySQL so I don't know much about performance.

function fetch_content($section, $subsection, &amp;$count, $page = 0){
    $section = substr($section, 0,8);
    $subsection = substr($subsection, 0,8);
    require_once("system/config.php");
    //Initiate connection 
        $mysqli = new mysqli($db_host, $db_user, $db_password, $db_database);
        if ($mysqli-&gt;connect_error) {
            die('Connect Error (' . $mysqli-&gt;connect_errno . ') '. $mysqli-&gt;connect_error);
        }
    //Select page
        $limit = 2;
        $start = $page * $limit ;

    //select query
        if($section == 'home' || ($section != 'home' &amp;&amp; $subsection == NULL)){
            $selection =  "WHERE section = ?";
            }
        else
            $selection = "WHERE section = ? AND subsection = ?";

    //Fetch data
        $stmt = $mysqli-&gt;stmt_init();
        $qry= "SELECT * FROM public
        $selection
        ORDER BY id DESC LIMIT ?,?";
        $stmt-&gt;prepare($qry);
        if($section == 'home' || ($section != 'home' &amp;&amp; $subsection == NULL))
            $stmt-&gt;bind_param("sss", $section, $start , $limit);
        else
            $stmt-&gt;bind_param("ssss", $section, $subsection, $start , $limit);
        $stmt-&gt;execute();
        $result = $stmt-&gt;get_result();
    //Format the data
        while( $row =  $result-&gt;fetch_assoc()){
            format_home($row, $mysqli);
        }
        $stmt-&gt;close();
    //Count result
        $stmt = $mysqli-&gt;stmt_init();
        $qry= "SELECT COUNT(*) AS count FROM public  $selection";
        $stmt-&gt;prepare($qry);
        if($section == 'home' || ($section != 'home' &amp;&amp; $subsection == NULL))
            $stmt-&gt;bind_param("s", $section);
        else
            $stmt-&gt;bind_param("ss", $section, $subsection);
        $stmt-&gt;execute();
        $result = $stmt-&gt;get_result();
        $count = $result-&gt;fetch_assoc();
        $count =  $count['count'];
        $stmt-&gt;close();
    //close connection
        $mysqli-&gt;close();
}
I would make a single file with the MySQL server connection, so you can use the same connection in other functions, etc..

Also you should pass string variables directly.

$stmt-&gt;prepare("SELECT COUNT(*) AS count FROM public  $selection");


Did you know, that you can check every single called function if a error occurred?
For example:

if(!($stmt-&gt;execute()) {
   die('Unable to execute!'. $mysqli-&gt;error);
}


The next point is, why do you execute another query to count the result rows?
In the mysqli_stmt object $num_rows. http://php.net/manual/en/mysqli-stmt.num-rows.php