Ex No: 12 Demonstrate the Database - Select Operation
Aim: To write a PHP script to demonstrate database Select operation.
Algorithm
Step1: Use the stud_db database with stud_table.
Step2: Get the student name to search fromt the stud_table.
Step3: Check if student name is present for deletion.
   A) Include the database class file.
   B) Create a new database object and get student name for search(selection).
   C) Call the select student function using object to get the data from the database.
   D) If student data is found display the data.
   E) else display "student name not found".
db_class.php
<?php
 class Database
 {
  var $hostadd;
  var $usr;
  var $pwd;
  var $DB;
  var $db_con;
  
  public function __construct()
  {
   $this->hostadd = "127.0.0.1";
   $this->usr = "root";
   $this->pwd = "";
   $this->DB= "stud_db";
   $this->db_con = mysqli_connect($this->hostadd,$this->usr,$this->pwd,$this->DB);
   
   if(mysqli_connect_errno())
   {
   	header("Location:error.php");
   	return mysqli_connect_errno();
   	exit();
   }
   else
   {
   	return 1;
   } 
  }
  
  public function select_stud($name)
  {
   $qry = "select roll_no, stud_name from stud_table where stud_name like '%$name%'";
   $res = mysqli_query($this->db_con,$qry) or die("Error in query");
   $numrows = mysqli_num_rows($res);
   $elm = "";
   if($numrows>0)
   {
   	while($row = mysqli_fetch_row($res))
   	{
   	 $elm = $elm."<tr><td>$row[0]</td><td>$row[1]</td></tr>";
   	}
   }
   return $elm;	
  }	
  
  public function __destruct()
  {
   mysqli_close($this->db_con);
  }
 }
?>
            
ex11.php
            
  
<html>
<head>
 <meta charset="utf-8">
 <title>Student Management System</title> 
 </head>
 <body> 
 <?php
  $style="";
  $resstyle="display: none;";
  if($_SERVER["REQUEST_METHOD"] == "POST")//if check data post
  {
   include("db_class.php");
   $db_obj = new Database();
   $stud_name = $_POST["stud_name"];
   
   $stud_data = $db_obj->select_stud($stud_name);
   if($stud_data!="")//if check db result
   {
    $style = "display: none;";
    $resstyle="";   	
   }   
   else
   {
 ?>

 <script>
  alert("student Name Not found");
 </script>

 <?php
   }//end if check db result
  }//end if check data post

 ?>
 
 <div style="<?php echo $resstyle; ?>">
 <h1><u>Search Result</u></h1>
  <table>
   <tr>
    <th>Roll No</th>
    <th>Name</th>
   </tr>
   <?php
    echo $stud_data;
   ?>
  </table>
 </div>
 <div style="<?php echo $style;?>">   
 <h1><u>Search Student by Name</u></h1>
 <form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>">
 <table>
  <tr>
   <td>Student Name:</td>
   <td><input type="text" name="stud_name" required="true" /></td>
  </tr>
  <tr>
   <td><input type="submit" value="Search"/></td>
   <td><input type="reset"/></td>
  </tr>
 </table> 
 </form>
 </div>  
 </body>
</html>
            
Result: Thus a database Select operation is demonstrated using a webpage with php script.