简体   繁体   English

我应该如何使用 PHP 和 SQL 将数据库中的值存储为 php 中的变量?

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

I would like to see where my code is incorrect.我想看看我的代码哪里不正确。 I want to store values from my database as a php array.我想将数据库中的值存储为 php 数组。 Then I'd like to store the individual parts of the array as separate variables.然后我想将数组的各个部分存储为单独的变量。 Here is my code:这是我的代码:

<?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];
?>

This will run your mysql query and add each comment to an array of comments, then print the array.这将运行您的 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);

    ?>

not sure if you are in console or through web server.不确定您是在控制台中还是通过 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';
}
?>

this is called a loop.这称为循环。 loop is your friend.循环是你的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM