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

时间:2017-06-15 21:35:17

标签: php mysql if-statement mysqli

我正在努力获得"轮次"从我的数据库中的列轮。所以,我希望得到每个辩论/帖子的字段值(1-5)。根据值/数字,它应该显示不同的东西。当我认为值为NULL时,即使该辩论/帖子的数据库字段中的值为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);
        }
    }

}
?> 

2 个答案:

答案 0 :(得分: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
?> 

答案 1 :(得分:0)

$result->fetch_row()只取一行结果。你必须在循环中调用它来获取所有行。

而不是所有重复的代码,使用循环来打印重复的<h2>元素,因为它们遵循一个简单的模式。

while ($row = $result->fetch_assoc()) {
    $round = $row['rounds'];
    for ($i = 1; $i <= $round; $i++) {
        echo "<h2 class="CommentHeading">Round $i (Pro)</h2> <br> <h2 class="CommentHeading">Round $i (Con) </h2>";
    }
    foreach ($Sender->Data('Answers') as $Row) {
        $Sender->EventArguments['Comment'] = $Row;
        WriteComment($Row, $Sender, Gdn::Session(), 0);
    }
}