Read Data From Database Using PHP — Demo Preview

Retrieve or Fetch Data From Database in PHP

As we know Database is a collection of tables that stores data in it.

To retrieve or fetch data from MySQL database it is simple to do it using MySQL ” Select ” query in PHP .

Here in this blog post we will be going to see how to fetch data and to display it in front end.

MySQL Select Query:

SELECT column_name(s) FROM table_name
$query = mysql_query("select * from tablename", $connection);

For this you must have a database in MySQL . Here, we have a database named “company” which consists of a table named “employee” with 5 fields in it.

Next we have created a PHP page named “updatephp.php” where following steps will be going to perform:

$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("company", $connection);
$query = mysql_query("select * from employee", $connection);
Name: E-mail: Contact No: Address: 

Below is our complete code with download and live demo option

PHP File: readphp.php

        

Read Data Using PHP

Click On Menu

"> "; echo "
"; > ?>

---Details---

Name: E-mail: Contact No: Address: > ?>

MySQL Code Segment: Here is the MySQL code for creating database and table.

 CREATE DATABASE company; CREATE TABLE employee( employee_id int(10) NOT NULL AUTO_INCREMENT, employee_name varchar(255) NOT NULL, employee_email varchar(255) NOT NULL, employee_contact varchar(255) NOT NULL, employee_address varchar(255) NOT NULL, PRIMARY KEY (employee_id) ) 

CSS File: style.css

 @import "http://fonts.googleapis.com/css?family=Droid+Serif"; /* Above line is to import google font style */ .maindiv < margin:0 auto; width:980px; height:500px; background:#fff; padding-top:20px; font-size:14px; font-family:'Droid Serif',serif >.title < width:100%; height:70px; text-shadow:2px 2px 2px #cfcfcf; font-size:16px; text-align:center; font-family:'Droid Serif',serif >.divA < width:70%; float:left; margin-top:30px >.form < width:400px; float:left; background-color:#fff; font-family:'Droid Serif',serif; padding-left:30px >.divB < width:100%; height:100%; background-color:#fff; border:dashed 1px #999 >.divD < width:200px; height:480px; padding:0 20px; float:left; background-color:#f0f8ff; border-right:dashed 1px #999 >p < text-align:center; font-weight:700; color:#5678C0; font-size:18px; text-shadow:2px 2px 2px #cfcfcf >.form h2 < text-align:center; text-shadow:2px 2px 2px #cfcfcf >a < text-decoration:none; font-size:16px; margin:2px 0 0 30px; padding:3px; color:#1F8DD6 >a:hover < text-shadow:2px 2px 2px #cfcfcf; font-size:18px >.clear < clear:both >span

Conclusion:
We have shown you how SELECT command of SQL is executed with PHP, for fetching data from database. For more MySQL commands with PHP, follow our other blog posts .

40 Replies to “Retrieve or Fetch Data From Database in PHP”

Dear,
You have told how to create database and table in this example “Retreive Data From Database using PHP”, but forgot to tell how to make the entries into it…

Hi Prakash , the below posted code is executed . just you change the database connection details as like your database.
// see this code :
————————————–
selectdata.php file
———————————— Read Data From Database Using PHP – Demo Preview Read Data Using PHP Click On Menu
$connection = mysql_connect(«localhost», «root», «[email protected]»); // Establishing Connection with Server
$db = mysql_select_db(«users», $connection); // Selecting Database
//MySQL Query to read data
$query = mysql_query(«select * from employee», $connection);
while ($row = mysql_fetch_array($query)) echo » “;
echo “”;
>
?> —Details—

Name:
E-mail:
Contact No:
Address: ———————————————————
css file :
————- @import “http://fonts.googleapis.com/css?family=Droid+Serif”;
/* Above line is to import google font style */
.maindiv margin:0 auto;
width:980px;
height:500px;
background:#fff;
padding-top:20px;
font-size:14px;
font-family:’Droid Serif’,serif
>
.title width:100%;
height:70px;
text-shadow:2px 2px 2px #cfcfcf;
font-size:16px;
text-align:center;
font-family:’Droid Serif’,serif
>
.divA width:70%;
float:left;
margin-top:30px
>
.form width:400px;
float:left;
background-color:#fff;
font-family:’Droid Serif’,serif;
padding-left:30px
>
.divB width:100%;
height:100%;
background-color:#fff;
border:dashed 1px #999
>
.divD width:200px;
height:480px;
padding:0 20px;
float:left;
background-color:#f0f8ff;
border-right:dashed 1px #999
>
p text-align:center;
font-weight:700;
color:#5678C0;
font-size:18px;
text-shadow:2px 2px 2px #cfcfcf
>
.form h2 text-align:center;
text-shadow:2px 2px 2px #cfcfcf
>
a text-decoration:none;
font-size:16px;
margin:2px 0 0 30px;
padding:3px;
color:#1F8DD6
>
a:hover text-shadow:2px 2px 2px #cfcfcf;
font-size:18px
>
.clear clear:both
>
span font-weight:700
>

I created a web page that take user detail and question and insert it into my database, but i can’t fetch the answer to back to their page without refreshing and update to keep sending back each answer to the question “id” can you please help me.
This is for school test

How to use select button in php instead of names in your example Retreive Data From Database using PHP
?
Please help me.

Источник

PHP MySQL Select Data

The SELECT statement is used to select data from one or more tables:

or we can use the * character to select ALL columns from a table:

To learn more about SQL, please visit our SQL tutorial.

Select Data With MySQLi

The following example selects the id, firstname and lastname columns from the MyGuests table and displays it on the page:

Example (MySQLi Object-oriented)

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDB»;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) die(«Connection failed: » . $conn->connect_error);
>

$sql = «SELECT id, firstname, lastname FROM MyGuests»;
$result = $conn->query($sql);

if ($result->num_rows > 0) // output data of each row
while($row = $result->fetch_assoc()) echo «id: » . $row[«id»]. » — Name: » . $row[«firstname»]. » » . $row[«lastname»]. «
«;
>
> else echo «0 results»;
>
$conn->close();
?>

Code lines to explain from the example above:

First, we set up an SQL query that selects the id, firstname and lastname columns from the MyGuests table. The next line of code runs the query and puts the resulting data into a variable called $result.

Then, the 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, firstname and lastname columns.

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

Example (MySQLi Procedural)

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDB»;

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) die(«Connection failed: » . mysqli_connect_error());
>

$sql = «SELECT id, firstname, lastname FROM MyGuests»;
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) // output data of each row
while($row = mysqli_fetch_assoc($result)) echo «id: » . $row[«id»]. » — Name: » . $row[«firstname»]. » » . $row[«lastname»]. «
«;
>
> else echo «0 results»;
>

You can also put the result in an HTML table:

Example (MySQLi Object-oriented)

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDB»;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) die(«Connection failed: » . $conn->connect_error);
>

$sql = «SELECT id, firstname, lastname FROM MyGuests»;
$result = $conn->query($sql);

if ($result->num_rows > 0) echo «

«;
// output data of each row
while($row = $result->fetch_assoc()) echo «

«;
>
echo «

ID Name
«.$row[«id»].» «.$row[«firstname»].» «.$row[«lastname»].»

«;
> else echo «0 results»;
>
$conn->close();
?>

Select Data With PDO (+ Prepared Statements)

The following example uses prepared statements.

It selects the id, firstname and lastname columns from the MyGuests table and displays it in an HTML table:

Example (PDO)

class TableRows extends RecursiveIteratorIterator <
function __construct($it) <
parent::__construct($it, self::LEAVES_ONLY);
>

function current() return «

» . parent::current(). «

«;
>

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDBPDO»;

try $conn = new PDO(«mysql:host=$servername;dbname=$dbname», $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare(«SELECT id, firstname, lastname FROM MyGuests»);
$stmt->execute();

Источник

How to Fetch and Display Data From Database in PHP in Table

Retrieve or fetch the data from the MySQL database in PHP and display table; In this tutorial, we will show you how to fetch/select/retrieve the data from the database in PHP and display it in the bootstrap Table.

First of all, we will create one database connecting file, And create html table web page with display data from database in PHP.

PHP Code Retrieve Data from the Database and Display in HTML Table

To retrieve data from MySQL, the SELECT statement is used. That can retrieve data from a specific column or all columns of a table.

If you want to get selected column data from the database. Use the below SQL query is:

SELECT column_name,column_name FROM table_name;

If you want to get all the columns data from a table. Use the below SQL query is:

PHP code to fetch data from MySQL database and display in HTML table

1. Connecting to the database in PHP

In this step, you will create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When we fetch, insert, update or delete data from MySQL database, there we will include this file:

2. Fetch data from the database and display in table

In this step, we will fetch the data from the MySQL database in PHP and display data in an HTML table. So you can create a new file and update the below code into your file.

The below code is used to retrieve or receive data from the MySQL database in PHP. Additionally, we will display the fetched data in an HTML table.

          .bs-example    

0) < ?>
Name Email id Mobile
?>
else < echo "No result found"; >?>

Conclusion

To retrieve or fetch or get the data from the MySQL database in PHP. Here you have learned how to fetch and display data in an HTML table using PHP MySQL.

This is a very basic and easy example of fetching the data into a MySQL database using PHP script. In the next tutorial, we will show you how you can update from data into the MySQL database in PHP.

Источник

Читайте также:  Java config java home
Оцените статью
Добавить комментарий