Database Read in PHP

Database read in php means you have a database table with records now you need to select these records and print it or use these records for some other purpose we will check how to read one record or many records.

Let's create a table, then we will add some records to it. i am going to use our previous created table in previous post http://jinnahsoft.net/learn/form-submit-in-php/

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `user_name` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

After running the above query, a table will be created in database in MySQL. Now we need to add some records to this table so we can read these records.

You may use phpMyAdmin to insert record or from my previous lecture form submit to add records or use below query if yet it is empty table.

INSERT INTO `users` (`id`, `user_name`, `password`, `email`, `phone`, `address`) VALUES
(1, 'Qadeer ahmad', '123', 'qadeer@gmail.com', '03216612463', 'arsal town jhelum'),
(2, 'Qadeer ahmad', '12345', 'qadeer2@gmail.com', '03216612463', 'arsal town jhelum'),
(3, 'Qadeer ahmad', '1234544', 'qadeer3@gmail.com', '03216612463', 'arsal town jhelum'),
(4, 'Qadeer ahmad', '123321', 'qadeer4@gmail.com', '03216612463', 'arsal town jhelum'),
(5, 'Qadeer ahmad', '12332144', 'qadeer5@gmail.com', '03216612463', 'arsal town jhelum');

above query will add 5 records in the table users. Now let move on our topic database read in php as we have table with 5 records if we are going to read one record, we have unique field id so let's create a page view_user.php offcouse we need to create to connection before work with database table.

<?php
$server_name = "localhost";
$db_user_name = "username";
$db_password = "password";

$db_name = "test";

// Create connection
$con = mysqli_connect($server_name , $db_user_name , $db_password, $db_name );

// Check connection
if (!$con) {
  die("Connection Error: " . mysqli_connect_error());
}

?>

The above code will create a connection and it will save in $con so we will use this connection variable to run the select query. let's make a select query to select one record then we print it for the test.

$sql="select * from users where id=2";

this query selecting the record having id value is 2 now we run this query and fetch this record.

$result = $con -> query($sql);
$row = $result -> fetch_array(MYSQLI_ASSOC);

as we are calling a unique id record so it is confirmed that it is only one record, but when we know there is multiple records then we will use a loop to fetch records one by one. let's get and print the fields one by one.

$ID=$row['id']; 
$userName=$row['user_name'];
$Password=$row['password'];
$Email=$row['email'];
$Phone=$row['phone'];
$Address=$row['address'];

as in select query we give * it means select all fields of id = 2 we may bound with some required fields like

$sql="select user_name,password,email from users where id=2";

now only these fields user_name, password and email will be selected and we can print only these three fields.

If we want to select all table record then we don't add where condition like.

$sql="select * from users";

Now all the records of table users will be select and we need to add loop when printing these records like.

$result = $con -> query($sql);
while($row = $result -> fetch_array(MYSQLI_ASSOC))
{
  $ID=$row['id']; 
  $userName=$row['user_name'];
  $Password=$row['password'];
  $Email=$row['email'];
  $Phone=$row['phone'];
  $Address=$row['address'];
  echo("ID". $ID, "<br />");
  echo("User Name". $username, "<br />");
  echo("Password". $Password, "<br />");
  echo("Email". $Email, "<br />");
  echo("Phone". $Phone, "<br />");
  echo("Address". $Address, "<br /><br />");
}

This will print all 5 records which are present in database now if you note i am printing inside the loop.

let's put all coding in one file to read all table fully.

<?php
$server_name = "localhost";
$db_user_name = "username";
$db_password = "password";
$db_name = "test";

// Create connection
$con = mysqli_connect($server_name , $db_user_name , $db_password, $db_name );

// Check connection
if (!$con) {
  die("Connection Error: " . mysqli_connect_error());
}
$sql="select * from users";
$result = $con -> query($sql);
while($row = $result -> fetch_array(MYSQLI_ASSOC))
{
  $ID=$row['id']; 
  $userName=$row['user_name'];
  $Password=$row['password'];
  $Email=$row['email'];
  $Phone=$row['phone'];
  $Address=$row['address'];
  echo("ID". $ID, "<br />");
  echo("User Name". $username, "<br />");
  echo("Password". $Password, "<br />");
  echo("Email". $Email, "<br />");
  echo("Phone". $Phone, "<br />");
  echo("Address". $Address, "<br /><br />");
}
?>