1
2<?php
3$localhost = "localhost";
4$username = "root";
5$password = "";
6$dbname = "samueldb";
7$con = new mysqli($localhost, $username, $password, $dbname);
8if( $con->connect_error){
9 die('Error: ' . $con->connect_error);
10}
11$sql = "SELECT * FROM users";
12if( isset($_GET['search']) ){
13 $name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
14 $sql = "SELECT * FROM users WHERE firstname ='$name'";
15}
16$result = $con->query($sql);
17?>
18<!DOCTYPE html>
19<html>
20<head>
21<title>Basic Search form using mysqli</title>
22<link rel="stylesheet" type="text/css"
23href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
24</head>
25<body>
26<div class="container">
27<label>Search</label>
28<form action="" method="GET">
29<input type="text" placeholder="Type the name here" name="search">
30<input type="submit" value="Search" name="btn" class="btn btn-sm btn-primary">
31</form>
32<h2>List of students</h2>
33<table class="table table-striped table-responsive">
34<tr>
35<th>ID</th>
36<th>First name</th>
37<th>Lastname</th>
38<th>Address</th>
39<th>Contact</th>
40</tr>
41<?php
42while($row = $result->fetch_assoc()){
43 ?>
44 <tr>
45 <td><?php echo $row['user_id']; ?></td>
46 <td><?php echo $row['firstname']; ?></td>
47 <td><?php echo $row['lastname']; ?></td>
48 <td><?php echo $row['address']; ?></td>
49 <td><?php echo $row['contact']; ?></td>
50 </tr>
51 <?php
52}
53?>
54</table>
55</div>
56</body>
57</html>
58
1$result = mysqli_query($con, "SELECT * FROM employees
2 WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'");
3
4while ($row = mysqli_fetch_array($result))
5{
6 echo $row['first_name'] . " " . $row['last_name'];
7 echo "<br>";
8}
9 mysqli_close($con);
10 ?>