Encoding multiple rows from prepared statement in php I have this code that hypothetically returns multiple rows from a database and encodes them in a JSON. I am wondering if this is the best and most presentable way to do it. 

Note: I am using msqli not PDO, and procedural php not OOP.

&lt;?php
$query = $_REQUEST["query"];
require(connect.php);

$response = array();
$response["success"] = false;

if ($con) {
    $statement = mysqli_prepare($con, "SELECT * FROM `lecturers_general` WHERE ? IN (lecturer_id, title, first_name, middle_name, last_name, course1, course2, course3, course4, course5, course6, office_number, office_building, department)");
    mysqli_stmt_bind_param($statement, "s", $query);
    mysqli_stmt_execute($statement);
    $lecturer_id = null; $title = null; $first_name = null; $middle_name = null; $last_name = null; $course1 = null; $course2 = null; $course3 = null; $course4 = null; $course5 = null; $course6 = null; $office_number = null; $office_building = null; $department = $null;

    mysqli_stmt_bind_result($lecturer_id, $title, $first_name, $middle_name, $last_name, $course1, $course2, $course3, $course4, $course5, $course6, $office_number, $office_building, $department); 
    // $rows = mysqli_fetch_assoc($statement);
    $num_rows = mysqli_num_rows($statement);        

    if ($num_rows &gt;= 1) {
        do {
            $lecturer_id = $rows["lecturer_id"];
            $title = $rows["title"];
            $first_name = $rows["first_name"];
            $middle_name = $rows["middle_name"];
            $last_name = $rows["last_name"];
            $course1 = $rows["course1"];
            $course2 = $rows["course2"];
            $course3 = $rows["course3"];
            $course4 = $rows["course4"];
            $course5 = $rows["course5"];
            $course6 = $rows["course6"];
            $office_number = $rows["office_number"];
            $office_building = $rows["office_building"];
            $department = $rows["department"];

            $result['lecturers: '][] = array('lecturer_id' =&gt; $lecturer_id,'title' =&gt; $title,'first_name' =&gt; $first_name,'middle_name' =&gt; $middle_name,'last_name' =&gt; $last_name,'course1' =&gt; $course1,'course2' =&gt; $course2,'course3' =&gt; $course3,'course4' =&gt; $course4,'course5' =&gt; $course5,'course6' =&gt; $course6,'office_number' =&gt; $office_number,'office_building' =&gt; $office_building,'department' =&gt; $department); 
        } while($rows = mysqli_fetch_assoc($statement));

        echo json_encode($result);
    } else {
        echo 'No results';
    }
}


?&gt;
I am using msqli not PDO, and procedural php not OOP.


While you should. 

Because it will make the whole mess into three blimmin' lines

&lt;?php
require 'connect.php';

$statement = $con-&gt;prepare("SELECT * FROM `lecturers_general` WHERE ? IN (lecturer_id, title, first_name, middle_name, last_name, course1, course2, course3, course4, course5, course6, office_number, office_building, department)");
$statement-&gt;execute([$_REQUEST["query"]]);
echo json_encode(['lecturers: ' =&gt; $statement-&gt;fetchAll()]);