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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了