检查数组是否为空,然后是while条件

时间:2013-11-21 00:10:04

标签: php

我有代码执行查询,获取结果,然后对这些结果执行操作。我想确保查询不返回空,否则while条件将失败。

$ckey = $GLOBALS['ckey'];
$query3 = "SELECT * FROM uc_users WHERE id = $ckey;";
$result3 = mysqli_query($dbc, $query3);

if(!empty(mysqli_fetch_array($result3))
{
  while($row3 = mysqli_fetch_array($result3))
  {
    $GLOBALS['cname'] = $row3['display_name'];
  }
}
else{ echo "empty";}

返回“致命错误:无法在C:中写入上下文中使用函数返回值”

$ckey = $GLOBALS['ckey'];
$query3 = "SELECT * FROM uc_users WHERE id = $ckey;";
$result3 = mysqli_query($dbc, $query3);

if(mysqli_num_rows(mysqli_fetch_array($result3)))
{
  while($row3 = mysqli_fetch_array($result3))
  {
    $GLOBALS['cname'] = $row3['display_name'];
  }     
}
else{ echo "empty";}

返回“警告:mysqli_num_rows()期望参数1为mysqli_result,在C中给出的数组:”

1 个答案:

答案 0 :(得分:0)

在你的第二个代码上试试这个:

if( mysqli_num_rows($result3) )