我想要count的值,并希望在其他查询中使用它。怎么可能?

时间:2016-03-25 12:20:34

标签: php mysql mysqli

这是我的问题:

$qry1 = "SELECT COUNT('name') FROM `brands` WHERE `catagory_id` = '".$id."';";
$res = mysqli_query($conn, $qry1);

我想要 count的值,并希望在另一个查询中使用它。怎么可能?

2 个答案:

答案 0 :(得分:1)

两个步骤: 1.为count(name)total计算的值提供有意义的名称,例如 2.使用名称total

获取结果的第一行并从1访问值
$qry1 = "SELECT COUNT(name) as total FROM `brands` WHERE `catagory_id` = '".$id."';";
$count = $mysqli->query($conn, $qry1)->fetch_object()->total;

答案 1 :(得分:0)

尝试;

$qry1 = "SELECT COUNT(name) as counts FROM brands WHERE catagory_id = '".$id."';";
$res = mysqli_query($conn, $qry1);
$row = mysqli_fetch_array($res);
$count = $row["counts"];

现在这个$ count变量保存了该值。您可以在其他查​​询中包含此变量。