php - 从列中获取值,但返回 null?

标签 php mysql

我正在尝试从数据库中的轮次列中获取“轮次”的值。所以,我想获取每个辩论/帖子的字段数字的值。根据值/数字,它应该显示不同的内容。当我应该得到这个值时,它说 NULL,即使该辩论/帖子的 db 字段中的值为 4。这不仅仅是那个辩论,而且所有辩论都会发生这种情况。如何获取列中字段的实际值并将其分配给名为 $rounds 的变量。该变量需要具有每次辩论的值,而不仅仅是该辩论的值。

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

$con = new mysqli($servername, $username, $password, $dbname);

if($con->connect_error)
{
  die("Connection failed: " . $con->connect_error);
}
else
{
  $sql = "SELECT rounds FROM vf_Discussion";
  $result = $con->query($sql);

  $allRounds = $result->fetch_row();
  $rounds = $allRounds[0];

  var_dump($rounds);
}

mysqli_close($con);

$rounds1 = '<h2 class="CommentHeading">Round 1 (Pro)</h2> <br> <h2 class="CommentHeading">Round 1 (Con) </h2>';
$rounds2 = '<h2 class="CommentHeading">Round 2 (Pro)</h2> <br> <h2 class="CommentHeading">Round 2 (Con)</h2>';
$rounds3 = '<h2 class="CommentHeading">Round 3 (Pro)</h2> <br> <h2 class="CommentHeading">Round 3 (Con)</h2>';
$rounds4 = '<h2 class="CommentHeading">Round 4 (Pro)</h2> <br> <h2 class="CommentHeading">Round 4 (Con)</h2>';
$rounds5 = '<h2 class="CommentHeading">Round 5 (Pro)</h2> <br> <h2 class="CommentHeading">Round 5 (Con)</h2>';

foreach($allRounds as $rounds)
{
  if($rounds == 1)
  {
    echo $rounds1;
    foreach($Sender->Data('Answers') as $Row)
    {
      $Sender->EventArguments['Comment'] = $Row;
      WriteComment($Row, $Sender, Gdn::Session(), 0);
    }
  }
  if($rounds == 2)
  {
    echo $rounds1;
    echo $rounds2;
    foreach($Sender->Data('Answers') as $Row)
    {
      $Sender->EventArguments['Comment'] = $Row;
      WriteComment($Row, $Sender, Gdn::Session(), 0);
    }
  }
  if($rounds == 3)
  {
    echo $rounds1;
    echo $rounds2;
    echo $rounds3;
    foreach($Sender->Data('Answers') as $Row)
    {
      $Sender->EventArguments['Comment'] = $Row;
      WriteComment($Row, $Sender, Gdn::Session(), 0);
    }
  }
  if($rounds == 4)
  {
    echo $rounds1;
    echo $rounds2;
    echo $rounds3;
    echo $rounds4;
    foreach($Sender->Data('Answers') as $Row)
    {
      $Sender->EventArguments['Comment'] = $Row;
      WriteComment($Row, $Sender, Gdn::Session(), 0);
    }
  }
  if($rounds == 5)
  {
    echo $rounds1;
    echo $rounds2;
    echo $rounds3;
    echo $rounds4;
    echo $rounds5;
    foreach($Sender->Data('Answers') as $Row)
    {
      $Sender->EventArguments['Comment'] = $Row;
      WriteComment($Row, $Sender, Gdn::Session(), 0);
    }
  }
}
?>

最佳答案

我用了PDO s。您的错误在于 $rounds 的分配。 我清理了您的代码以提高可读性:

<?php
$servername = "";
$dbname = "";
$username = "";
$password = "";

$pdo = NULL;

try
{
  $pdo = new PDO('mysql:host=' . $servername . ';dbname=' . $dbname, $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
  die("Connection failed: " . $exception->getMessage());
}

$rounds = []; // array we want to save all the rounds to

// the database
$query = "SELECT rounds, COUNT(*) AS cnt FROM vf_Discussion GROUP BY rounds ORDER BY rounds";
$statement = $pdo->prepare($query);
$statement->execute();

$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);

foreach($rows as $row)
{
  $rounds[] = ['name' => $row['rounds'], 'count' => $row['cnt']];
}

foreach($rounds as $round)
{
  $name = $round['name'];
  $cnt = $round['cnt'];

  echo '<h2 class="CommentHeading">Round ' . $round . ' (Pro)</h2> <br> <h2 class="CommentHeading">Round ' . $round . ' (Con)</h2> <br> <h2 class="CommentHeading">Number of Rounds ' . $cnt . '</h2>';

  foreach($Sender->Data('Answers') as $Row)
  {
    $Sender->EventArguments['Comment'] = $Row;
    WriteComment($Row, $Sender, Gdn::Session(), 0);
  }
}
?>

关于php - 从列中获取值,但返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574090/

相关文章:

php - 处理多个表单输入字段

mysql - 事务隔离级别很好的解释

php - 积极识别来自 java HttpClient 的 http 请求

php - 在mysql中更新用户信息的问题

php - while 循环重复相同的数据

javascript - 如何使大文本字段的文本框变宽

mysql - Spring Boot + JPA[Hibernate 支持] + mysql = 数据截断 : Data too long for column 'ATTRIBUTE_NAME' at row 1

php - 如果出现多行,如何在 php mysql 的侧存储过程中执行选择查询?

php - 如何在结账页面后将用户重定向回index.php?

PHP 为什么我只能将 1 个数据插入我的数据库(mysql)