【问题标题】:Retrieve database entry with mysqli使用 mysqli 检索数据库条目
【发布时间】:2018-01-06 09:03:38
【问题描述】:

拥有以下PHP:

<?php
session_start();
$conn = mysqli_connect("localhost", "test", "", "test");

if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT status FROM updates";
if ($result = mysqli_query($conn, $sql)) {
    while ($row = mysqli_fetch_row($result)) {
        echo '<div class="statusRow">' . "Status: " . $row['status'] . '</div>';
    }
    mysqli_free_result($result);
}
mysqli_close($conn);
?>

上面没有返回所需的“状态”输入,只是空白。不确定我在这里缺少什么?

【问题讨论】:

  • 基本错误检查和显示。
  • @KevinP 输出是“状态:”
  • 将 $row['status'] 更改为 $row[0]
  • @KevinP 100% 确定。我已经检查了四次。我在while循环中添加print_r($row['status']),现在返回“状态:1”。不确定 1 指的是什么?
  • @KevinP 更改为 $row[0] 似乎已经成功了。 0 到底是什么意思?

标签: php mysqli


【解决方案1】:

问题是你使用mysqli_fetch_row,它返回数字数组。 您需要切换到 mysqli_fetch_assoc。 然后您将能够通过字符串 $row['status']

访问

http://php.net/manual/en/mysqli-result.fetch-assoc.php

【讨论】:

    【解决方案2】:

    $row['status'] 更改为$row[0] 或使用mysqli_fetch_assoc()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多