search in php mysql with examples – You can make an HTML form that allows you to find or search all the database records in your MySQL Tables as well as print the all the results in a web browser.
How to create search option or feature using PHP? – In these web project we use LIKE as well as wildcards for the query therefor that users do not have to have an exact match for data results to be contributed.
In this example we search the members data by name, age or many more.
search.html
<html>
<body>
<form action="do_action.php" method="post">
Search <input type="text" name="query_str"><br>
<input type ="submit">
</form>
</body>
</html>
do_action.php
<?php
$query_str = $_POST['query_str'];
$servername = "localhost";
$username = "geogit";
$password = "985689";
$db = "classDB";
$link = new mysqli($servername, $username, $password, $db);
if ($link->connect_error){
die("Connection failed: ". $link->connect_error);
}
$qq_sql = "select * from members where name like '%$query_str%'";
$result = $link->query($qq_sql);
if ($result->num_rows > 0){
while($results = $result->fetch_assoc() ){
echo $results["name"]." ".$results["age"]." ".$results["type"]."<br>";
}
} else {
echo "0 records";
}
$link->close();
?>
searchoption.html
<html>
<body>
<form action="do_actionOption.php" method="post">
Search <input type="text" name="query_str"><br>
Column: <select name="column">
<option value="name">Name</option>
<option value="age">Age</option>
<option value="type">Type</option>
</select><br>
<input type ="submit">
</form>
</body>
</html>
do_actionOption.php
<?php
$query_str = $_POST['query_str'];
$column = $_POST['column'];
$servername = "localhost";
$username = "geogit";
$password = "985689";
$db = "classDB";
$link = new mysqli($servername, $username, $password, $db);
if ($link->connect_error){
die("Connection failed: ". $link->connect_error);
}
$qq_sql = "select * from members where $column like '%$query_str%'";
$result = $link->query($qq_sql);
if ($result->num_rows > 0){
while($results = $result->fetch_assoc() ){
echo $results["name"]." ".$results["age"]." ".$results["type"]."<br>";
}
} else {
echo "0 records";
}
$link->close();
?>