How To Select Data From a MySQL Database Using PHP?

Step 1: Connect to MySQL Database

<?php

$DatabaseServer = "localhost";
$DatabaseUsername = "root";
$DatabasePassword = "root";
$DatabaseName = "demo";

$Connection = mysqli_connect($DatabaseServer , $DatabaseUsername ,  $DatabasePassword , $DatabaseName);

if($Connection === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

?>

Step 2: Fetch Data From MySQL Database

<?php

$SQL = "SELECT * FROM user";
$Result = mysqli_query($Connection, $SQL);
while($Row = mysqli_fetch_assoc($Result)
    {
   echo $FirstName = $Row['FirstName '];
   echo $Email = $Row['Email'];
    }

?>

Mysql: Run these queries

CREATE TABLE `user` (
  `UserID` int(12) NOT NULL,
  `FirstName` varchar(48) NOT NULL,
  `LastName` varchar(48) NOT NULL,
  `Email` varchar(128) NOT NULL,
  `City` varchar(48) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `user` (`UserID`, `FirstName`, `LastName`, `Email`, `City`) VALUES
(1, 'Ram', 'Sri', 'zzz@zzz.com', 'Delhi'),
(2, 'Krishna', 'Vasudev', 'yyy@yyy.com', 'Delhi');

Please find the below link to know how to set up a local server(XAMPP) and creating a database.
http://www.phpjavascript.com/2018/01/how-to-set-up-local-server-using-xampp.html


No comments:

Post a Comment

Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Popular Posts