MYSQLI多查询功能

MYSQLI multi query function

我想创建一个自动连接到数据库并执行给定查询的函数,但我无法让它工作,而且它没有给出任何错误。 我想我没有以正确的方式输出我的目标是输出一个数组来存储查询的所有返回值。

这是我到目前为止的代码,希望你能帮忙:

public function db_query() {
    $ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/app.ini');
    $mysqli = new mysqli($ini['db_location'], $ini['db_user'], $ini['db_password'], $ini['db_name']);

    // create string of queries separated by ;
    $query = "SELECT name FROM mailbox;";
    $query .= "SELECT port FROM mailbox";

    // execute query - $result is false if the first query failed
    $result = mysqli_multi_query($mysqli, $query);

    if ($result) {
        do {
            // grab the result of the next query
            if (($result = mysqli_store_result($mysqli, 0)) === false && mysqli_error($mysqli) != '') {
                echo "Query failed: " . mysqli_error($mysqli);
                while ($row = $result->fetch_row()) {
                    echo $row[0];
                }
            }
        } while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
    } else {
        echo "First query failed..." . mysqli_error($mysqli);
    }
}

Note: I did not add the parameter for the query just for testing purposes.

public function db_query($mysqli) {

    $return = [];
    $result = mysqli_query($mysqli, "SELECT name FROM mailbox");
    while ($row = $result->fetch_row()) {
        $return[] = $row[0];
    }
    $result = mysqli_query($mysqli, "SELECT port FROM mailbox");
    while ($row = $result->fetch_row()) {
        $return[] =  $row[0];
    }
    return $return;
}

简单、干净、高效、始终有效