You can obtain the results of a MySQL server query using the mysqli extension.
In this example, the code first establishes a connection to the MySQL server using the mysqli
extension. It then executes a SELECT query to retrieve all the data from a table named table_name
. The code uses a while
loop to iterate over the result set and outputs each row as a row in the HTML table. The HTML table has three columns for the id
, name
, and age
fields.
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Execute the query $sql = "SELECT * FROM table_name"; $result = $conn->query($sql); // Check if there are any results if ($result->num_rows > 0) { echo "<table><tr><th>ID</th><th>Name</th><th>Age</th></tr>"; // Output the data for each row while($row = $result->fetch_assoc()) { echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["age"]. "</td></tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?>