簡體   English   中英

我應該如何使用 PHP 和 SQL 將數據庫中的值存儲為 php 中的變量?

[英]How am I supposed to store a value from my database as a variable in php using PHP and SQL?

我想看看我的代碼哪里不正確。 我想將數據庫中的值存儲為 php 數組。 然后我想將數組的各個部分存儲為單獨的變量。 這是我的代碼:

<?php
    $result = mysqli_query($db, "SELECT column FROM table");
    if (!$result) {
        echo 'Could not run query';
        exit;
    }
    $comments = mysqli_fetch_row($result);
    $comment0 = $comments[0];
    $comment1 = $comments[1];
    $comment2 = $comments[2];
    $comment3 = $comments[3];
    $comment4 = $comments[4];
    $comment5 = $comments[5];
    $comment6 = $comments[6];
    $comment7 = $comments[7];
    $comment8 = $comments[8];
    $comment9 = $comments[9];
?>

這將運行您的 mysql 查詢並將每個評論添加到一個評論數組中,然后打印該數組。

   <?php
        $result = mysqli_query($db, "SELECT column FROM table");
        if (!$result) {
            echo 'Could not run query';
            exit;
        }
        $comments = array();
        while($comment = mysqli_fetch_row($result)){
            $comments[] = $comment;
        }
        print_r($comments);

    ?>

不確定您是在控制台中還是通過 Web 服務器。

<?php
    $result = mysqli_query($db, "SELECT column FROM table");
    if (!$result) {
        echo 'Could not run query';
        exit;
    }
    $comments = mysqli_fetch_row($result);

foreach($comments as $comment){
    echo print_r($comment,1).'--------------\r\n<br>\r\n';
}
?>

這稱為循環。 循環是你的朋友。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM