我在使用mysqli多查询时遇到了一些麻烦,特别是在它们出现之后使用它们。如果我在其中放入'printf'行将打印平均值,但我不能使用似乎使用mysqli部分之外的数组[计划使用它们来组织下面页面上的图像]。
我对PHP和Mysql很陌生,所以如果我错过了先前线程中的某些东西,我会道歉,这些东西会对此进行排序/指向正确的方向 - 但我仍然在努力解决它。
如果我对此感到愚蠢,那么随意放火烧我,把我扔进一桶粪便,但是之前有'printf(“%s \ n”,$ row [0,1,2] ,3等]);'在它中将打印结果,但如果我稍后在页面中尝试使用row [0],它将使用这些字符而不是调用应该存储在那里的值。
$mysqli=new mysqli("localhost", "uname", "passwordbit", "mydb");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT AVG(cpcriminalswork) from mydb;";
$query .= "SELECT AVG(cpsentancing) from mydb;";
$query .= "SELECT AVG(cpdeathpenalty) from mydb;";
$query .= "SELECT AVG(cphumanrights) from mydb;";
$query .= "SELECT AVG(cpprotection) from mydb;";
$query .= "SELECT AVG(tbtaxationspread) from mydb;";
$query .= "SELECT AVG(tbtaxmax) from mydb;";
$query .= "SELECT AVG(tbdole) from mydb;";
$query .= "SELECT AVG(tbchildben) from mydb;";
$query .= "SELECT AVG(tbunemployment) from mydb;";
$query .= "SELECT AVG(eebanks) from mydb;";
$query .= "SELECT AVG(eeminwage) from mydb;";
$query .= "SELECT AVG(eeforiegnaid) from mydb;";
$query .= "SELECT AVG(eemilitary) from mydb;";
$query .= "SELECT AVG(eemaxwage) from mydb";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
}
$result->free();
}
if ($mysqli->more_results()) {
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
答案 0 :(得分:0)
不需要Multiquery。你可以用SQL收集AVG。
(见http://www.sqlfiddle.com/#!2/529e8/3)
$query = "SELECT AVG(foo1) as foo1, AVG(foo2) as foo2, AVG(foo3) as foo3, AVG(foo4) as foo4 from mydb";
$res = mysqli_fetch_array(mysqli_query($query));
print_r($res);
输出应该类似
foo1 => 3 foo2 => 1 foo3 => 7 foo4 => 9