PHP和Mysqli获取count(distinct())查询的值 [英] PHP and Mysqli getting value of a count(distinct()) query

查看:114
本文介绍了PHP和Mysqli获取count(distinct())查询的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mysql中有一个名为gsm的数据库表,其中有2列LAC和NAME.

I have a db table in mysql called gsm, with 2 columns, LAC and NAME.

因此,我试图计算数据库中存储了多少个不同的LAC,并检索了一个php值以供进一步使用.我正在使用mysqli

So I am trying to count how many different LAC are stored in the DB and retrieve a php value to further use. I am using mysqli

我有:

$sql = "select count(distinct lac) from gsm ");

如何将查询存储到php中的变量中?

How do I store that query into a variable in php?

推荐答案

//conection:
$link = mysqli_connect("www.mywebsite.com","user","password","dataname") or die("Error " . mysqli_error($link));

//consultation:
$query = "SELECT COUNT(DISTINCT alias) as visitors FROM Visits where time BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 1 HOUR)) AND timestamp(NOW())";

//execute the query.
$result = $link->query($query) or die("Error in the consult.." . mysqli_error($link));
$row = mysqli_fetch_array($result);

//display information:
// The text to draw
$text = $row['visitors'];

这篇关于PHP和Mysqli获取count(distinct())查询的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆