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

时间:2019-01-14 14:51:36

标签: php mysql web

嗯,我一直在仅基于通过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>";  

 } 

 ?>

Database table

1 个答案:

答案 0 :(得分:-1)

好的,因此在您进行编辑时,在将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>

我希望这会有所帮助!

相关问题