Show values from a MySQL database table inside a HTML table on a webpage
Example taken from W3Schools: PHP Select Data from MySQL
<?php$con=mysqli_connect("example.com","peter","abc123","my_db");// Check connectionif (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();}$result = mysqli_query($con,"SELECT * FROM Persons");echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";while($row = mysqli_fetch_array($result)){echo "<tr>";echo "<td>" . $row['FirstName'] . "</td>";echo "<td>" . $row['LastName'] . "</td>";echo "</tr>";}echo "</table>";mysqli_close($con);?>
It's a good place to learn from!
Try this: (Completely Dynamic...)
<?php$host = "localhost";$user = "username_here";$pass = "password_here";$db_name = "database_name_here";//create connection$connection = mysqli_connect($host, $user, $pass, $db_name);//test if connection failedif(mysqli_connect_errno()){ die("connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")");}//get results from database$result = mysqli_query($connection,"SELECT * FROM products");$all_property = array(); //declare an array for saving property//showing propertyecho '<table class="data-table"> <tr class="data-heading">'; //initialize table tagwhile ($property = mysqli_fetch_field($result)) { echo '<td>' . $property->name . '</td>'; //get field name for header array_push($all_property, $property->name); //save those to array}echo '</tr>'; //end tr tag//showing all datawhile ($row = mysqli_fetch_array($result)) { echo "<tr>"; foreach ($all_property as $item) { echo '<td>' . $row[$item] . '</td>'; //get items using property value } echo '</tr>';}echo "</table>";?>
Object-Oriented with PHP/5.6.25 and MySQL/5.7.17 using MySQLi [Dynamic]
Learn more about PHP and the MySQLi Library at PHP.net.
First, start a connection to the database. Do this by making all the string variables needed in order to connect, adjust them to fit your environment, then create a new connection object with new mysqli()
and initialize it with the previously made variables as its parameters. Now, check the connection for errors and display a message whether any were found or not. Like this:
<?php$servername = "localhost";$username = "root";$password = "yourPassword";$database = "world";mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);$conn = new mysqli($servername, $username, $password, $database);echo "Connected successfully<br>";?>
Next, make a variable that will hold the query as a string, in this case its a select
statement with a limit
of 100 records to keep the list small. Then, we can execute it by calling the mysqli::query()
function from our connection object. Now, it's time to display some data. Start by opening up a <table>
tag through echo
, then fetch one row at a time in the form of a numerical array with mysqli::fetch_row()
which can then be displayed with a simple for loop. mysqli::field_count
should be self explanatory. Don't forget to use <td></td>
for each value, and also to open and close each row with echo"<tr>"
and echo"</tr>
. Finally we close the table, and the connection as well with mysqli::close()
.
<?php$query = "select * from city limit 100;";$queryResult = $conn->query($query);echo "<table>";while ($queryRow = $queryResult->fetch_row()) { echo "<tr>"; for($i = 0; $i < $queryResult->field_count; $i++){ echo "<td>$queryRow[$i]</td>"; } echo "</tr>";}echo "</table>";$conn->close();?>