首页 > 解决方案 > 在现有函数中使用 mysqli_multi_query

问题描述

我有一个可以建立不同类型的数据库连接的函数,下面简化为只有两个这样的 $ActionType 值,这是我遇到困难的第二个。没有错误,但也没有结果。有任何想法吗?

该查询包含多个查询,例如:

$Query = "SQL statement #1;";
$Query .= "SQL statement #2;";
$Query .= "SQL statement #3;";

这就是功能,减少到适合这里:

function DBConnect($Query, $ActionType, $DBname, $selType='array') {
    $ActionType = trim(strtolower($ActionType));

    if (!$Query) :
        exit();
    endif;

    $MySQLError = "";
    $mysqli = dbConn($DBname);

    if ($Query && $mysqli->connect_errno != 0):
        $MySQLError = "<div class=\"ErrorMessage\">";
        $MySQLError .= printf("Connect failed: %s\n", $mysqli->connect_error);
        $MySQLError .= "</div>\n\n";
        return $MySQLError;
        exit();
    endif;

    switch ($ActionType) :
        case "multiple":
            if ($result = $mysqli->query($Query)) :
                $numrowsCat = $result->num_rows;
                if ($numrowsCat >= 1) :
                    $result = $mysqli->query($Query);
                    if ($selType === "assoc") :
                        while($row = $result->fetch_assoc()) :
                            $results_array[] = $row;
                        endwhile;
                    else :                  
                        while($row = $result->fetch_array()) :
                            $results_array[] = $row;
                        endwhile;
                    endif;
                    return $results_array;
                endif;
                $MySQLError = ($mysqli->connect_errno) ? mysqli_error($mysqli) : "";
                $mysqli->close();
                if ($MySQLError) return $MySQLError;
            endif;
        break;

        case "multiquery":
            if ($result = $mysqli->multi_query($Query)) :
                if ($result) :
                    while($row = $result->fetch_array()) :
                        $results_array[] = $row;
                    endwhile;
                    return $results_array;
                endif;

                $MySQLError = ($mysqli->connect_errno) ? mysqli_error($mysqli) : "";
                $mysqli->close();
                if ($MySQLError) return $MySQLError;
            endif;
        break;

    endswitch;
}

标签: phpmysqlimysqli-multi-query

解决方案


推荐阅读