PHP MySQLi Select Data
PHP Tutorial » PHP MySQLi Select Data
To select data from DataBase we use the SELECT statement
Syntax:
or we use the " * " character(without quotes) to select ALL columns from a table:
Select (view) Data With MySQLi from DataBase
MySQLi Object-oriented method
The next example selects the id, user_name and email columns from the Members table and displays it on the page:
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}$sql = "SELECT id, user_name, email FROM Members";
$result = $link->query($sql);if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - User Name:
" . $row["user_name"]. " - E_mail: "
. $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
The function num_rows() checks if database (my_DB) are more than zero rows returned.
If my_DB are more than zero rows returned, the function fetch_assoc() add all results into an associative array that can loop through.
The while() loop loops through the result set and all datas as id, user_name and email columns.
Related subjects:
Connect to mysqli
Delete file in PHP
PHP simple login
In this chapter we go deeperinto:
How read data from MySQL database in PHP?
How can I see data in MySQL table?
How will you select View display data from database with php MySQL?
How can I see PHP database?
Tags: php mysql: view data, display data in html table, display data in grid, display data from two tables, display data horizontally, display data in multiple pages, display mysql data in html form, display hierarchical data
php mysql select: large data, row data, data between two dates, data and spleat on pages, data intoo array, data from multiple pages, pdo, data from two tables
php mysql limit data selection
Select (view) Data With MySQLi from DataBase
MySQLi Procedural method
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";// Create connection
$link = mysqli_connect($server_name, $user_name,
$password, $db_name);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}$sql = "SELECT id, user_name, email FROM Members";
$result = mysqli_query($link, $sql);if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - User Name:
" . $row["user_name"]. " - E_mail:
" . $row["email"]. "<br>";
}
} else {
echo "0 results";
}mysqli_close($link);
?>
Select (view) Data With PDO from DataBase
PDO method
PDO extension. Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "my_username", "my_password". "my_DB")
<?php
try{
$pdo = new PDO("mysql:host=localhost;dbname=my_DB",
"my_username", "my_password");
// Set PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
// Attempt select query execution
try{
$sql = "SELECT * FROM Members";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>User_name</th>";
echo "<th>E_mail</th>";
echo "</tr>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
unset($result);
} else{
echo "No records matching your query were found.";
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. "
. $e->getMessage());
}
// Close connection
unset($pdo);
?>
php mysql: view data, display data in html table, display data in grid, display data from two tables, display data horizontally, display data in multiple pages, display mysql data in html form, display hierarchical data
php mysql select: large data, row data, data between two dates, data and spleat on pages, data intoo array, data from multiple pages, pdo, data from two tables
php mysql limit data selection
PHP MySQLi Select, View Data - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.