从准备好的语句返回多行。 (程序php)

时间:2018-07-20 22:14:48

标签: php json mysqli

我一直在尝试从php中的准备好的语句中收集多行...但是无济于事。我对此进行了几次尝试:使用foreachdo-whilemysqli_fetch_assoc处理mysqli_stmt_fetchmysqli_fetch_field循环。我还浏览了各种答案,例如:herehere,但它们对我没有用。

请注意我的代码与其他答案有何不同...我正在请求一个查询,该查询将用于通过IN搜索数据库,同时也使用了过程php和mysqli

这是我目前的尝试:

<?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);
        $result = array($statement);
        $meta_statement = mysqli_stmt_result_metadata($statement);

        while($field = mysqli_fetch_field($meta_statement)) {
        $result[] = &$lecturer_id[$lecturer_id -> lecturer_id];
        $result[] = &$title[$title -> title];
        $result[] = &$first_name[$first_name -> first_name];
        $result[] = &$middle_name[$middle_name -> middle_name];
        $result[] = &$last_name[$last_name -> last_name];
        $result[] = &$course1[$course1 -> course1];
        $result[] = &$course2[$course2 -> course2];
        $result[] = &$course3[$course3 -> course3];
        $result[] = &$course4[$course4 -> course4];
        $result[] = &$course5[$course5 -> course5];
        $result[] = &$course6[$course6 -> course6];
        $result[] = &$office_number[$office_number -> office_number];
        $result[] = &$office_building[$office_building -> office_building];
        $result[] = &$department[$department -> department];
    }

    call_user_func_array('mysqli_stmt_bind_result', $result);
    $lecturers = array();

    for($i = 0; $row === mysqli_stmt_fetch($statement); $i++) {
        $lecturers[$i] = array();

        foreach($data as $key => $value) {
            $lecturers[$i][$key] = $value;
        }
    }

    if (count($lecturers) >= 1) {
        echo json_encode(array('lecturers: ' => $lecturers));
    } else {
        echo 'No results';
    }
    } else {
        echo 'No connection';
    }
?>

0 个答案:

没有答案