从列中获取值,但返回null? [英] getting the value from column, but returns null?

查看:40
本文介绍了从列中获取值,但返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中的列回合中获取回合"的值.因此,我想获取一个值,该值是每个辩论/帖子的字段数.根据值/数字,应该显示不同的内容.当我假设获得该值时,即使该辩论/帖子的db字段中的值为4,它也表示为NULL.这不仅是辩论,而且对所有这些人都是这样.如何获取列中字段的实际值并将其分配给名为$ rounds的变量.这个变量需要具有针对每次辩论的价值,而不仅仅是该辩论.

I'm trying to get the value of "rounds" from the column rounds in my data base. So, I want to get the value which is a number of a field for each debate/post. Based on the value/number it is supposed to display a different thing. When I supposedly get the value it says NULL even though the value in the db field for that debate/post is 4. This is not just that debate, but this occurs for all of them. How can I get the actual value of the field in the column and assign it to a variable called $rounds . This variable needs to have the value for each debate made, not just that debate.

<?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的分配中. 并且我清理了您的代码以提高可读性:

I used PDOs. Your mistake was in the assignment of $rounds. And I cleaned up your code for better readability:

<?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);
  }
}
?>

这篇关于从列中获取值,但返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆