2017-06-06 45 views
0

我是php中的新成员,我被一个查询从表tree中提取所有记录而不计空记录。这里是我的代码:如何在mysql中使用count函数

$query = mysqli_query($con, "SELECT COUNT('left') FROM tree WHERE userid='$eid' AND left not null"); 

while ($result = mysqli_fetch_array($query)) { 
    print_r($result); 
} 

但我得到这个:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

+1

的可能的复制[mysqli的\ _fetch \ _array()/ mysqli的\ _fetch \ _assoc()/ mysqli的\ _fetch \ _row()预计参数1是资源或mysqli \ _result,布尔给定](https://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – CD001

回答

0

您所查询的是错的,你错过了IS在查询

mysqli_query($con,"SELECT COUNT(`left`) FROM tree WHERE userid='$eid' AND left not null"); 

更改为

mysqli_query($con,"SELECT COUNT(`left`) FROM tree WHERE userid='$eid' 
AND 
left IS not null"); 

以及在mysqli_query()之后粘贴以下代码以查看错误。

if (!$q) { 
    printf("Error: %s\n", mysqli_error($con)); 
    exit(); 
} 
+0

m geeting this错误:您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,在第1行的'is not null'附近使用正确的语法 –

+0

好的,那么在答案中提到的查询中使用非空。 – Narayan

+0

亚米使用不为空然后它显示错误 –

0

你缺少is操作。条件应该是left IS not null

但是请注意,是你申请的count功能上字符串文字'left',而不是在列left。如果你将它应用到列中,您可以删除空性状态,因为聚合函数跳过null S:

$q = mysqli_query($con,"SELECT COUNT(left) FROM tree WHERE userid='$eid'"); 
0

可以使用此代码

$q = mysqli_query($con,"SELECT COUNT(`left`) FROM tree WHERE userid='$eid' 
    AND 
    left IS NOT NULL"); 

    if(!q){ 
     while($r = mysqli_fetch_array($q)){ 
     print_r($r); 
     } 
    } 
+0

代码不起作用 –

+0

你是否面临同样的错误? –

+0

是的,我面临同样的问题 –

相关问题