Php Mysqli如何获取所有数据
我有一个疑问:Php Mysqli如何获取所有数据,php,mysql,mysqli,Php,Mysql,Mysqli,我有一个疑问: $q = "SELECT SUM(cena) AS total, MONTH( datum ) AS mesec FROM nakupi GROUP BY mesec"; $po = $mysqli->query($q); $mesecna_prodaja = $po->fetch_assoc(); 此查询正在从数据库中选择月度销售数据。如果我在phpMyAdmin中运行相同的查询,我会得到正确的结果。所有销售月份,包括每个月的总销售额。问题是,fet_assoc之
$q = "SELECT SUM(cena) AS total, MONTH( datum ) AS mesec
FROM nakupi
GROUP BY mesec";
$po = $mysqli->query($q);
$mesecna_prodaja = $po->fetch_assoc();
此查询正在从数据库中选择月度销售数据。如果我在phpMyAdmin中运行相同的查询,我会得到正确的结果。所有销售月份,包括每个月的总销售额。问题是,fet_assoc之后的$mesecna_prodaja在上个月只返回了一个结果?如何像phpMyAdmin那样获取所有数据?我尝试获取所有结果,但它不起作用(内部服务器错误)。我不确定您是如何查看结果的,但您需要遍历刚刚获取的关联数组以查看所有结果 尝试类似于中描述的内容
我不确定您是如何查看结果的,但您需要遍历刚刚获取的关联数组以查看所有结果 尝试类似于中描述的内容
当您发现mysqli_fetch_assoc确实为您获取了一行数据时,为了获得所有结果,您必须遍历结果集中的所有行 有很多方法可以做到这一点,但我将向您展示while循环:
$q = "SELECT SUM(cena) AS total, MONTH( datum ) AS mesec FROM nakupi GROUP BY mesec";
$po = $mysqli->query($q);
//$mesecna_prodaja = $po->fetch_assoc(); //This only gets the first row
//Setup an array to hold all the results
$allMyResults = array();
//Iterate through all the results until there's nothing left (which will cause the while condition to fail)
while ($row = $po->fetch_assoc()) {
//Add $row to allMyResults
$allMyResults[] = $row;
}
var_dump($allMyResults); //You should see all your results here.
这里的关键原则是迭代/遍历所有结果是[代码]的责任
ref:当您发现mysqli\u fetch\u assoc确实为您获取了一行数据,为了获得所有结果,您必须遍历结果集中的所有行 有很多方法可以做到这一点,但我将向您展示while循环:
$q = "SELECT SUM(cena) AS total, MONTH( datum ) AS mesec FROM nakupi GROUP BY mesec";
$po = $mysqli->query($q);
//$mesecna_prodaja = $po->fetch_assoc(); //This only gets the first row
//Setup an array to hold all the results
$allMyResults = array();
//Iterate through all the results until there's nothing left (which will cause the while condition to fail)
while ($row = $po->fetch_assoc()) {
//Add $row to allMyResults
$allMyResults[] = $row;
}
var_dump($allMyResults); //You should see all your results here.
这里的关键原则是迭代/遍历所有结果是[代码]的责任
ref:您可以使用
mysqli\u resit::fetch\u all()
或者你可以使用
$arr = [];
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$arr[] = $result->fetch_assoc();
}
} else {
echo "0 results";
}
您可以使用
mysqli\u resit::fetch\u all()
或者你可以使用
$arr = [];
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$arr[] = $result->fetch_assoc();
}
} else {
echo "0 results";
}
fetch\u assoc()
应该只从结果中提取一行。获取多个结果通常是通过在while循环中使用fetch方法来完成的。fetch\u assoc()
应该只从结果中获取一行。获取多个结果通常是通过在while循环中使用fetch方法来完成的。fetch\u assoc()
应该只从结果中获取一行。获取多个结果通常是通过在while循环中使用fetch方法来完成的。这对您有用吗?如果是这样,你应该投票并选择一个答案。如果没有,让我们知道,这样我们就可以试着找出是否还有其他事情发生。这对你有用吗?如果是这样,你应该投票并选择一个答案。如果没有,让我们知道,这样我们就可以试着找出是否还有其他事情发生。这对你有用吗?如果是这样,你应该投票并选择一个答案。如果没有,请让我们知道,以便我们可以尝试了解是否还有其他情况。