PHP从MySQL获取信息

PHP从MySQL获取信息

因此,顺便说一下,我想通过派别ID获得成员的昵称。但它不是印刷名称。我的代码有什么问题?当我打印$ nickname时,也没有给我任何东西。

<?php
require 'db.php';
session_start();

$result = $mysqli->query("SELECT nickname FROM members WHERE fraction='3'");

print($result->num_rows);
?>
  <div class="form">

      <ul class="tab-group">
        <li class="tab"><a href="#lspd">LS-PD</a></li>
        <li class="tab active"><a href="#ballas">BALLAS</a></li>
      </ul>

      <div class="tab-content">
        <div id="ballas">
          <h1>Ballas</h1>

          <form action="index.php" method="post" autocomplete="off">

          <div class="top-row">
            <div class="field-wrap">
              <label>
                Name<span class="req">*</span>
              </label>
            </div>
          </div>
          <?php
          if ($result->num_rows > 0)
          {
            while($row = $result->fetch_assoc())
            {
              echo "<p class='monitoring-name'>" . $row['nickname'] . "</p><br>";
            }
          }
          ?>
          </form>
        </div>
      </div><!-- tab-content -->

</div> <!-- /form -->

最佳答案

这可能对您有帮助。添加检查以确保您的查询正确,因此如果查询正确,它将运行代码,否则将生成错误:

$query = "SELECT nickname FROM members WHERE fraction = 3";
if ($result = $mysqli->query($query))
{
    if ($result->num_rows > 0)
    {
        while($row = $result->fetch_assoc())
        {
            echo "<p class='monitoring-name'>" . $row['nickname'] . "</p><br>";
        }
    }
    else
    {
        echo "No results found"; // pretty clear?
    }
}
else
{
    // if there's an error with your query it will display this message:
    echo "There was a problem with your query";
}

关于php - PHP从MySQL获取信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44957214/

10-12 00:02