繁体   English   中英

如何从数据库中检索数据并通过PHP以表格格式显示?

[英]How to retrieve data from database and display it in a table format through PHP?

好吧,我一直致力于仅基于通过 GET 方法提供的 Regno 和 Subcode 来检索数据。

我确实尝试了没有表格格式的获取输出,只是通过使用 printf stmt 。

<?php
            $regNo=$_GET['Regno'];
            $Subjectcode=$_GET['Subjectcode']; 

(然后是连接部分)

$sql="SELECT Co1,Co2, Co3, Co4, Co5, Co6 FROM ab where Regno='$regNo' and 
Subcode='$Subjectcode'";
            $result=mysqli_query($con,$sql);



            // Associative array
            $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

            ?>
            <div>
                <table >
                <tr>
                    <th>Co1</th>
                    <th>Co2</th>
                    <th>Co3</th>   // these get displayed 
                    <th>Co4</th>
                    <th>Co5</th>
                    <th>Co6</th>
                </tr>

                <?php
                If (mysqli_num_rows($result) > 0) {
                        while ($row = mysqli_fetch_array($result)) {
                    ?>
                <tr>    
                        <td><?php echo $row['Co1']; ?></td> 
                        <td><?php echo $row['Co2']; ?></td> 
                        <td><?php echo $row['Co3']; ?></td>  // these don't get displayed
                        <td><?php echo $row['Co4']; ?></td> 
                        <td><?php echo $row['Co5']; ?></td> 
                        <td><?php echo $row['Co6']; ?></td>
                    </tr>
                    <?php
                    }
                    }
                    ?>

                </tr>
                </table>
                </div>

输出应该是以表格格式显示的数据。 但我得到的输出什么都没有!!!

编辑 1

我也试过这个代码

<?php

error_reporting(E_ALL ^ E_DEPRECATED);  

// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'all';

// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}  

$reg_no = isset($_POST['Regno']) ? $_POST['Regno'] : '';
$sql = "SELECT * FROM ab WHERE Regno='".$reg_no."'";

while ($rows = mysqli_fetch_array($sql)) { 
     $Sem = $rows['Sem'];
     $Co1 = $rows['Co1'];
     $Co2 = $rows['Co2'];
     $Co3 = $rows['Co3'];
     $Co4 = $rows['Co4'];
     $Co5 = $rows['Co5'];
     $Co6 = $rows['Co6'];
     $Subname = $rows['Subname'];
     $Externalmarks = $rows['Externalmarks'];

     echo 


 "$Subname<br>$Sem<br>$Co1<br>$Co2<br>$Co3<br>$Co4<br>$Co5<br>$Co6<br>
 $Externalmarks<br><br>";  

 } 

 ?>

数据库表

好的,所以在您的编辑中,您在将 SQL 传递到 mysqli_fetch_array() 之前没有使用 mysqli_query(),因此它不会返回任何结果。

这里有一些参考资料: http : //php.net/manual/en/mysqli-result.fetch-array.php

<?php

error_reporting(E_ALL ^ E_DEPRECATED);  

// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'all';

// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}  
// Get the variable needed for the query. URL should look like ex: something.com/thispage.php?Regno=1220
$reg_no = isset($_GET['Regno']) ? $_GET['Regno'] : ''; 
$sql = "SELECT * FROM ab WHERE Regno='" . $reg_no . "'";
$query = $conn->query($sql);

?>

<div>
    <table >
        <tr>
            <th>Co1</th>
            <th>Co2</th>
            <th>Co3</th>
            <th>Co4</th>
            <th>Co5</th>
            <th>Co6</th>
        </tr>
<?php
    // Place each result into an array.
    $rows = array();
    while($row = $query->fetch_array()){
        $rows[] = $row;
    }

    // Loop through the array and structure the table.
    foreach($rows as $row){
        echo "<tr>";
        echo "<td>" . $row['Co1'] . "</td>";
        echo "<td>" . $row['Co2'] . "</td>";
        echo "<td>" . $row['Co3'] . "</td>";
        echo "<td>" . $row['Co4'] . "</td>";
        echo "<td>" . $row['Co5'] . "</td>";
        echo "<td>" . $row['Co6'] . "</td>";
        echo "</tr>";
    }

    // Free the result set. 
    $query->close();

    // Close the connection. 
    $conn->close();
 ?>
    </table>
</div>

我希望这有帮助!

暂无
暂无

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

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