Fetch Data from database

Fetch data from database :


Reading data from database is easy process .To fetch or read data from database using php you need to require some knowledge of php language and its functions and how you can enter records in mysql or insert operation or just enter the following sql file in sql tab of database as instructed in video i am using 'php operations' as database or you may create it accordingly.



Sql file to read data from database


CREATE TABLE `crud` (
`id` int(11) NOT NULL,
`firstName`
varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`gender` varchar(255) NOT NULL,
`age` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `crud` (`id`, `firstName`, `lastName`, `gender`, `age`, `email`) VALUES
(1, 'jeff', 'morgan', 'male', '42', 'jeff@gmail.com'),
(2, 'stenly', 'uba', 'female', '21', 'stenly@gmail.com'),
(3, 'pablo', 'morgan', 'male', '98', 'pablo@gmail.com'),
(4, 'susan', 'arya', 'female', '22', 'susan@yahoo.com');
ALTER TABLE `crud` ADD PRIMARY KEY (`id`);
ALTER TABLE `crud`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;


After creating a table into database the real logic behind the scene comes into play.First of all we require a connection to connect to mysql database so here is connection.php file which will use mysql_select_db for selecting database and mysql_connect function for connecting php to database and die function if there is no connection or no such database will exist than it will terminate the connection.


<?php
 $connection = mysqli_connect('localhost', 'root', '');
  if (!$connection){
  die("Database Connection Failed" . mysqli_error($connection));
  }
  $selectDb = mysqli_select_db($connection, 'php_operations');
  if (!$selectDb){
  die("Unable to select database" . mysqli_error($connection));
  }
?>



When connection.php file is ready , we are good to go and can fetch data from database

Fetch or Read data from database :


To read record from database we need following logic , first we have to know that while reading data from tables what is going behind actually we are selecting rows from tables and than displaying the result with the help of php. So the sql operation will be select a record.Than there are some php functions that will play their roles. mysql_query will execute sql commend and return result to result variable in the script.This will select all our table as a resource than we will use mysql_fetch_assoc function that will fetch the result as a associative array in key and value form and return result as row.Than we can display this array result with the help of while loop in the forms of tables or we can customize this result as we wish to display it but in most cases we require in tables.Here is "display.php" file that will display the whole record to fetch records from database.



<!DOCTYPE html>
<html>
<head>
<title>Read data from mysql - CRUD</title>
<!-- Latest compiled bootstrap for layout and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
 <body>
  <div class="
container">
  <div class="
row">
  <h1><center>Fetch Operation using php - crud</center></h1>


  <br/><hr/>
   <table class="
table ">
    <thead>
     <tr>
      <th>#</th>
      <th>Full Name</th>
      <th>E-Mail</th>
      <th>Age</th>
      <th>Gender</th>
      <th>Update Record</th>
      <th>Delete Record</th>
    </tr>
    </thead>
     <tbody>
  <?php
    require_once('connection.php');
    $query = "SELECT * FROM `crud`";
    $result = mysqli_query($connection, $query);
    while($row = mysqli_fetch_assoc($result)){
  ?>
   <tr>
   <th scope="row"><?php echo $row['id']; ?></th>
     <td><?php echo $row['firstName'] . " " . $row['lastName']; ?>       </td>
     <td><?php echo $row['email']; ?></td>
     <td><?php echo $row['gender']; ?></td>
     <td><?php echo $row['age']; ?></td>
     <td>
       <a href="update.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>
     </td>
    <td>
      <a href="delete.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
    </td>
   </tr>
  <?php } ?>
 </tbody>
 </table>
 </div>
</div>
</body>
</html>



If have any question related to fetch record from database kindly comment in comment-box.

No comments:

Post a Comment