1<?php
2$con=mysqli_connect("example.com","peter","abc123","my_db");
3// Check connection
4if (mysqli_connect_errno())
5{
6echo "Failed to connect to MySQL: " . mysqli_connect_error();
7}
8
9$result = mysqli_query($con,"SELECT * FROM Persons");
10
11echo "<table border='1'>
12<tr>
13<th>Firstname</th>
14<th>Lastname</th>
15</tr>";
16
17while($row = mysqli_fetch_array($result))
18{
19echo "<tr>";
20echo "<td>" . $row['FirstName'] . "</td>";
21echo "<td>" . $row['LastName'] . "</td>";
22echo "</tr>";
23}
24echo "</table>";
25
26mysqli_close($con);
27?>
28
1<?php
2$host = "localhost";
3$user = "username_here";
4$pass = "password_here";
5$db_name = "database_name_here";
6
7//create connection
8$connection = mysqli_connect($host, $user, $pass, $db_name);
9
10//test if connection failed
11if(mysqli_connect_errno()){
12 die("connection failed: "
13 . mysqli_connect_error()
14 . " (" . mysqli_connect_errno()
15 . ")");
16}
17
18//get results from database
19$result = mysqli_query($connection,"SELECT * FROM products");
20$all_property = array(); //declare an array for saving property
21
22//showing property
23echo '<table class="data-table">
24 <tr class="data-heading">'; //initialize table tag
25while ($property = mysqli_fetch_field($result)) {
26 echo '<td>' . $property->name . '</td>'; //get field name for header
27 array_push($all_property, $property->name); //save those to array
28}
29echo '</tr>'; //end tr tag
30
31//showing all data
32while ($row = mysqli_fetch_array($result)) {
33 echo "<tr>";
34 foreach ($all_property as $item) {
35 echo '<td>' . $row[$item] . '</td>'; //get items using property value
36 }
37 echo '</tr>';
38}
39echo "</table>";
40?>
41