Fetching data from database using php/MySql
In last tutorial we have seen about how to insert data into database using php and mysql. now we are going to discuss about how to Fetch/Select data from database using php/mysql.
In this tutorial we are fetching four values these are,
User_id, First_name, Last_name and Email.
here i used select * from user query for Retrieving/Fetching data from database.
in this above query it will select all fields from user table.
The if($result = mysqli_query($link, $sql)) is used to check successful execution of above query.
The if(mysqli_num_rows($result) > 0) is used to check and display the resultant matching rows other wise it will shows no data found.
include ("connection.php");function which is used to call database connection.
In this tutorial we are fetching four values these are,
User_id, First_name, Last_name and Email.
here i used select * from user query for Retrieving/Fetching data from database.
in this above query it will select all fields from user table.
The if($result = mysqli_query($link, $sql)) is used to check successful execution of above query.
The if(mysqli_num_rows($result) > 0) is used to check and display the resultant matching rows other wise it will shows no data found.
include ("connection.php");function which is used to call database connection.
Select.php
<?php
/* Database connection */
$link = mysqli_connect("localhost", "demo1", "demo123", "demo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM user";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>user_id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "<table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records found .";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
/* Database connection */
$link = mysqli_connect("localhost", "demo1", "demo123", "demo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM user";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>user_id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "<table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records found .";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
No comments:
Post a Comment