Welcome to findnerd, today we are going to discuss fetch data from database in php .
In this blog you can fetch data from database .
Firstly establish a database connection
Use the mysqli( ) function to established connection to the MySQL server.
<?php
$servername = "localhost"; // Here write the servername or an IP address
$username = "abc"; // Here write the username of your phpmyadmin
$password = "123456"; // Here write the password of your phpmyadmin password
$databasename = "databasename"; // Here write the database name of your database
// Create a connection from your databse
$Getconnection = new mysqli($servername, $username, $password, $databasename);
// Check connection
if ($Getconnection->connect_error) {
die("Connection not successful: " . $Getconnection->connect_error);
}
// Get data from database in city table
$GetData = "SELECT id, citycode, cityname FROM city";
$result = $Getconnection->query($GetData);
if ($result->num_rows > 0) {
// Get output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - citycode: " . $row["citycode"]. " -cityname: " . $row["cityname"]. "<br>";
}
} else {
echo "empty data";
}
$Getconnection->close();
?>
0 Comment(s)