php - 从每个字段的列中获取值,但它返回 NULL?

标签 php mysql if-statement mysqli

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

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

<?php    
      $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>';
       ?>

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

}
?> 

最佳答案

您能否尝试以下操作,看看是否能达到您想要的结果? 我已经包含了准备好的语句,而不是您的(不安全)查询方式。

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


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

if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}
else{
    $stmt = $con->prepare("SELECT rounds FROM `vf_Discussion`");
    $stmt->execute();
    $stmt->bind_result($rounds);
    while($stmt->fetch()) {

        $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>';

        if ($rounds == 1) {
            echo $rounds1;
            foreach ($Sender->Data('Answers') as $Row) {
                $Sender->EventArguments['Comment'] = $Row;
                WriteComment($Row, $Sender, Gdn::Session(), 0);
            }//end foreach
        }//end if
        if ($rounds == 2) {
            echo $rounds1;
            echo $rounds2;
            foreach ($Sender->Data('Answers') as $Row) {
                $Sender->EventArguments['Comment'] = $Row;
                WriteComment($Row, $Sender, Gdn::Session(), 0);
            }//end foreach
        }//end if
        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);
            }//end foreach
        }//end if
        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);
            }//end foreach
        }//end if
        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);
            }//end foreach
        }//end if

    }//end while
}//end else
?> 

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

相关文章:

php - 将 preg_replace() 替换为 preg_replace_callback()

php - preg_match_all 在使用克拉 (^) 时不匹配

mysql - MYSQL 中的批量插入

MySQL根据开始日期和结束日期之间的日期显示值

ios - 单元格中有多个按钮,可将选定的按钮设置为单元格数据

javascript - 为什么 if..else block 只能工作一次

php - FFmpeg - PHP 错误代码 127

php - Codeigniter 和 oracle 未定义常量 : OCI_COMMIT_ON_SUCCESS

php - 应用程序在提取数据时挂起

python - 如何在 python 中的 if 语句中测试异常?