PHP无法打印最低的mysqli ID号

时间:2018-07-11 14:59:30

标签: php mysqli

以下代码未打印出数据库表中的最低编号。当前有4个条目-仅打印3个。最低的ID号,不会打印。

代码:

### Run Query
$query = "SELECT * from `Reminders`";
$run = mysqli_query($connection, $query);
$check = mysqli_fetch_array($run);
### Fetch Data
if($check > 0)
{
  while($row = mysqli_fetch_assoc($run))
  {
    $ID = $row['ID'];
    $Text = $row['rText'];
    $Number = $row['rNumber'];
    $Date = $row['rDate'];
    $Time = $row['rTime'];

    echo "'$Text' to '$Number' on $Date at $Time.<br/>";
  }
}

2 个答案:

答案 0 :(得分:1)

删除$check部分,这是最低的部分。要计算返回的行数,请使用mysqli_num_rows()函数。

### Run Query
$query = "SELECT * from `Reminders`";
$run = mysqli_query($connection, $query);
// Change here... :)
$check = mysqli_num_rows($run);
### Fetch Data
if ($check > 0) {
  while ($row = mysqli_fetch_assoc($run)) {
    $ID = $row['ID'];
    $Text = $row['rText'];
    $Number = $row['rNumber'];
    $Date = $row['rDate'];
    $Time = $row['rTime'];
    echo "'$Text' to '$Number' on $Date at $Time.<br/>";
  }
}

答案 1 :(得分:1)

从php手册:-http://php.net/manual/en/mysqli-result.num-rows.php

  

mysqli_num_rows()函数用于返回行数   在结果集中。

所以,只需替换这段代码,

$check = mysqli_fetch_array($run);

此行,

$check = mysqli_num_rows($run);