AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQLi Select Data


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP MySQLi Select Data

To select data from DataBase we use the SELECT statement

Syntax:

SELECT column_name(s) FROM table_name

or we use the " * " character(without quotes) to select ALL columns from a table:

SELECT * FROM table_name

 

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:

Example (MySQLi Object-oriented)

<?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

Example (MySQLi Procedural)

<?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")

Example (PDO display data in html table)
<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...