php - 在现有函数中使用 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;
}
解决方案
推荐阅读
- python - Python字符串转换,去掉空格,加连字符
- angular - Angular 6 - 检查图像 url 的 http 状态
- python - 在 Anaconda Spyder 中运行我的代码时出现 FailedPreconditionError
- java - 有没有办法通过使用 Intellij 添加外部方法调用来重构?
- javascript - 我应该在我的列表页面上的过滤器之前添加跳过链接吗?
- javascript - Javascript mongoDB 采用变量名而不是变量的值
- javascript - 如何在 php、ajax、javascript(无 jquery)中从数据库中删除数据
- java - 如何将数据集保存到 orc 文件的不同部分
- pycharm - UnicodeDecodeError:“ascii”编解码器无法解码位置 32 中的字节 0xe1:序数不在范围内(128)
- apache-nifi - NiFi - 如何解决更新文件并发问题