doushi3189 2015-02-01 02:32
浏览 40
已采纳

在PHP中显示sql表中的所有数据?

When I print my code it only prints the question and description of id = 1 but not the rest of the table.

here is my code.

Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.

Thanks So Much!

<?php

require_once "connection.php";

if(isset($_POST['submit'])) {

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );

    if($conn->connect_error) {

        die("connection error: " . $conn->connect_error);
    } else {

        echo "Submit button connected to database!";
    }

    $question = $_POST['question'];
    $description = $_POST['description'];

    $sql = " INSERT INTO `ask` (question_id, question, description) VALUES
    (NULL, '{$question}', '{$description}' ) ";

    if($conn->query($sql)) {

        echo "it worked";
    } else {

        echo "error: " . $conn->error;

        exit();
    }


    $query = "SELECT * FROM `ask` ";

    if( $result = $conn->query($query)) {
        $fetch = $result->fetch_assoc();

        echo "<p>{$fetch['question']}</p>";
        echo "<p>{$fetch['description']}</p>";

    } else {
        echo "failed to fetch array";
    }






}


?>
  • 写回答

2条回答 默认 最新

  • douzhuan4406 2015-02-01 02:38
    关注

    You need a for each loop:

        <?php
    
    require_once "connection.php";
    
    if(isset($_POST['submit'])) {
    
        $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
    
        if($conn->connect_error) {
    
            die("connection error: " . $conn->connect_error);
        } else {
    
            echo "Submit button connected to database!";
        }
    
        $question = $_POST['question'];
        $description = $_POST['description'];
    
        $sql = " INSERT INTO `ask` (question_id, question, description) VALUES
        (NULL, '{$question}', '{$description}' ) ";
    
        if($conn->query($sql)) {
    
            echo "it worked";
        } else {
    
            echo "error: " . $conn->error;
    
            exit();
        }
    
    
        $query = "SELECT * FROM `ask` ";
    
        if( $result = $conn->query($query)) {
            $fetch = mysql_fetch_array($result, MYSQL_ASSOC);
            foreach($fetch as $ques) {
                echo "<p>" . $ques['question'] . "</p>";
                echo "<p>" . $ques['description'] . "</p>";
            }
    
        } else {
            echo "failed to fetch array";
        }
    
    
    
    
    
    
    }
    
    
    ?>
    

    All I've done there is change:

    $fetch = $result->fetch_assoc();
    
    echo "<p>{$fetch['question']}</p>";
    echo "<p>{$fetch['description']}</p>";
    

    to:

    $fetch = mysql_fetch_array($result, MYSQL_ASSOC);
    foreach($fetch as $ques) {
        echo "<p>" . $ques['question'] . "</p>";
        echo "<p>" . $ques['description'] . "</p>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)