Php 如何在mysqli_fetch_assoc中避免空值?
我需要从数据库中获取值。 我正在使用MySQLPhp 如何在mysqli_fetch_assoc中避免空值?,php,mysql,mysqli,Php,Mysql,Mysqli,我需要从数据库中获取值。 我正在使用MySQLmysqli\u fetch\u assoc和while循环,我不知道列的名称 我正在从行中获取null值 这是我的密码: $myQuery ='Select * from `tableName` '; $query = $conn->query($myQuery); while($row = mysqli_fetch_assoc($query)) { $data[] = $row; } 当我打印这个$data[]数组时
mysqli\u fetch\u assoc
和while循环
,我不知道列的名称
我正在从行中获取null
值
这是我的密码:
$myQuery ='Select * from `tableName` ';
$query = $conn->query($myQuery);
while($row = mysqli_fetch_assoc($query))
{
$data[] = $row;
}
当我打印这个$data[]
数组时,有几个空行如下所示:
{
ID: null,
Car: null,
Phone: null,
State: null
},
{
ID: null,
Car: null,
Phone: null,
State: null
},
如何避免这些空行?我不知道列名 在MySQL中,我们可以使用IFNULL()函数查找空值。试试这个
$myQuery ='Select IFNULL(Car,0),IFNULL(Phone,0),IFNULL(State,0) from `tableName` ';
(or)
$myQuery ='Select IFNULL(*,0) from `tableName` ';
select*from tableName where ID not null
你一直说你不知道列名,但你只是将四个列名打印了两次。只需在查询过程中过滤空值:select*from tableName where Car not null
?还有,为什么数据库中有空值。您应该避免插入所有空值可能的重复项。除了“不知道列的名称”注释之外,您可以使用descripe tableName
在MySQL中构建表。