Bagaimana cara mengambil data dari database MySQL menggunakan PHP?

0
(0)

<?php

   $servername = "localhost";

   $username = "root";

   $password = "";

   $dbname = "gfg";

    

   

   $conn = new mysqli($servername,$username,$password,$dbname);

    

    

    if ($conn -> connect_errno)

    {

       echo "Failed to connect to MySQL: " . $conn -> connect_error;

       exit();

    }

 

    $sql = "select * from student";

    $result = ($conn->query($sql));

    

    $row = []; 

 

    if ($result->num_rows > 0) 

    {

        

        $row = $result->fetch_all(MYSQLI_ASSOC);  

    }   

?>

 

<!DOCTYPE html>

<html>

<style>

    td,th {

        border: 1px solid black;

        padding: 10px;

        margin: 5px;

        text-align: center;

    }

</style>

 

<body>

    <table>

        <thead>

            <tr>

                <th>Name</th>

                <th>Branch</th>

                <th>Roll Number</th>

            </tr>

        </thead>

        <tbody>

            <?php

               if(!empty($row))

               foreach($row as $rows)

              

            ?>

            <tr>

 

                <td><?php echo $rows['name']; ?>

                <td><?php echo $rows['branch']; ?>

                <td><?php echo $rows['roll_no']; ?>

 

            </tr>

            <?php } ?>

        </tbody>

    </table>

</body>

</html>

 

<?php   

    mysqli_close($conn);

?>

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.