php - 在两个不同的 while 循环中添加数字

标签 php mysql

我在两个不同的 while 循环中从数据库中获取数据,我想在循环外添加它们之间的变量。示例:

while($cash_fetch = mysql_fetch_array($cash_qur))
{
      $a = 500; //suppose I am fetching from database
}

while($card_fetch = mysql_fetch_array($card_qur))
{
    $b = 1000; //suppose I am fetching from database
}

$total = $a+$b;

echo $total;

我确实想做这件事,但得到的结果不合适。请帮忙。

最佳答案

你可以试试这个 -

$a = $b = 0; // set to 0 by default
while($cash_fetch = mysql_fetch_array($cash_qur))
{
    $a += 500; //Increment
}

while($card_fetch = mysql_fetch_array($card_qur))
{
    $b += 1000; //Increment
}
$total = $a + $b;
echo $total;

关于php - 在两个不同的 while 循环中添加数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909200/

相关文章:

php - SQL IN() 可以查询 TAKE OR 语句吗

php - 不想刷新并保留最后插入的值

mysql - 修改查询以使其执行速度更快,但仍然得到相同的结果

mysql - 使用一个 SELECT 的结果作为第二个 SELECT 的输入,然后进行 COUNT

php - mysqli php 选择最近的条目 asc

javascript - 如何在 javascript 定时器/循环中运行 php 代码

php - 多个文件存在检查?更好的方法?

php - 绑定(bind)到 PDO 查询的参数数量不正确

php - 如何从一对多关系中使用 SQL 获取平均税率

php - 每天用 PHP 重置 MySQL 值