HOME | PHP Tutorial | CSS
facebook icon
AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP
PHP Tutorial

PHP forms
HTML Tutorial

CSS Tutorial

Display data from SQL database with MySQLi into php - html table


OK, you have a database on phpmyadmin (sql) and you want to display one of table into a table on HTML PHP.

Select Data With MySQLi and display - how to create - example


In the following example selects the id, name and surname columns from the users table and displays it on the page:

Example 1: (MySQLi Object-oriented)

<?php
$servername = "localhost";
$username = "your-username";
$pass = "your-password";
$dbname = "my-DB";

// Create connection
$link = new mysqli($servername, $username, $pass, $dbname);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);

echo "<table>";
$sql = "SELECT id, name, surname FROM Users order by rand() limit 4";
$result = $link->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. " </td><td>" ". $row["name"]. "</td><td> " . $row["surname"]. " </td></tr> ";
}
} else {
echo "0 results";
}

echo "</table>";
$conn->close();
?>

function - num_rows() - checks if there are more than zero rows returned.

If there are more than zero rows returned, the function -fetch_assoc() - puts all the results into an associative array that we can loop through. The - while() loop - loops through the result set and outputs the data from the id, name and surname columns.

Note: SELECT column_name(s) FROM table_name - - or we can use the * character to select ALL columns from a table: SELECT * FROM table_name

MySQLi procedural way: Example - How to create

The following example shows the same as the example above, in the MySQLi procedural way:

Example 2: (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "your-username";
$pass = "your-password";
$dbname = "my-DB";

// Create connection
$link = mysqli_connect($servername, $username, $pass, $dbname);
// Check connection
if (!$link) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "<table>";
$sql = "SELECT id, name, surname FROM Users order by rand() limit 4";
$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["id"]. " </td><td>" ". $row["name"]. "</td><td> " . $row["surname"]. " </td></tr> ";
}
} else {
echo "0 results";
}

echo "</table>";
$conn->close();
?>

The results -

Display data from SQL, database with MySQLi, php html table Display data from SQL database with MySQLi into php - html table - sql tutorial

3290. SQL delete row - SQL delete table, sql tutorial
SQL delete row - SQL delete table sql tutorial
https://www.agernic.com/sql-tutorial/sql-delete-row-sql-delete-table.html

2574. SQL Hosting - SQL Tutorial

http://www.sql-tutorial.net/sql-hosting.asp

3284. PHP mysqli_query() Function - mysqli query, sql tutorial
PHP mysqli_query() Function - mysqli query sql tutorial
https://www.agernic.com/sql-tutorial/php-mysqli_query-function-database-mysqli.html

363. SQL Tutorial
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, and XML.
https://www.w3schools.com/sql/default.asp

3286. SQL BETWEEN Operator, sql tutorial
SQL BETWEEN Operator sql tutorial
https://www.agernic.com/sql-tutorial/sql-between-operator-with-example.html

2563. SQL Aliases - SQL Tutorial

http://www.sql-tutorial.net/SQL-Aliases.asp

Your add here
PHP isset() Function

PHP rtrim() function

PHP strlen() function

PHP strcmp() function

PHP print() function

PHP lcfirst() function

PHP implode() function

PHP strip_tags() function

PHP wordwrap() function

PHP str_repeat() function

PHP strstr() function

PHP define() function

PHP md5() function

PHP strrchr() Function

PHP str_replace() function

PHP htmlentities() function

CSS background image size to fit full screen
How to prevent sql injection in php mysqli
HTML Image Align
HTML Introduction
Deprecated: mysql_connect() mysql extension is deprecated and will be
HTML Font Face