0

我有两个来自 sql 的计数查询。我想划分这两个查询的结果。我试过了:

    $con = mysqli_connect("localhost", "root", "", "xoxo") or die(mysqli_connect_error());

    $query = "SELECT COUNT(xxxx) AS a zzzz";

    $query1 = "SELECT COUNT(xxxx) AS b FROM zzzz WHERE xxxx = 'cccc'";


    $result = mysqli_query($con, $query) or die('Error querying database');
    $result1 = mysqli_query($con, $query1) or die('Error querying database');

    $row = mysqli_fetch_array($result);
    $rowa = mysqli_fetch_array($result1);

    $x = $row/$rowa;

    echo $x;

但它告诉我它有一个致命的错误。我也试过:

$con = mysqli_connect("localhost", "root", "", "xoxo") 或 die(mysqli_connect_error());

    $query = "SELECT COUNT(xxxx) AS row1 FROM zzzz";

    $query1 = "SELECT COUNT(xxxx) AS row2 FROM zzzz WHERE xxxx = 'cccc'";

    $result = mysqli_query($con, $query) or die('Error querying database');
    $result1 = mysqli_query($con, $query1) or die('Error querying database');


    while ($row = mysqli_fetch_array($result)) {

    }

    while ($row1 = mysqli_fetch_array($result1)) {

    }

    echo (int)($row['row1'] / $row1['row2']);

但它告诉我这是除以零。但是,我的 row1 和 row2 都对它有价值。

4

2 回答 2

2

当您使用while循环时,这意味着它将循环直到条件为假,这意味着您的$row$row1都是假的。你需要的是:

$query = "SELECT COUNT(xxxx) AS row1 FROM zzzz";

$query1 = "SELECT COUNT(xxxx) AS row2 FROM zzzz WHERE xxxx = 'cccc'";

$result = mysqli_query($con, $query) or die('Error querying database');
$result1 = mysqli_query($con, $query1) or die('Error querying database');

$row = mysqli_fetch_array($result);
$row1 = mysqli_fetch_array($result1);

echo (int)($row['row1'] / $row1['row2']);

您的第一个示例不起作用,因为当您$row/$rowa尝试划分 2 个数组时...

于 2013-05-22T14:04:44.887 回答
0

您已经初始化了两个查询的结果while loop

尝试这个

 $row = mysqli_fetch_array($result);
 $row1 = mysqli_fetch_array($result1);

echo (int)($row['row1'] / $row1['row2']);
于 2013-05-22T14:07:23.430 回答