search in php mysql with examples – How to Create a Search Feature with PHP and MySQL?

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.

search in php mysql Select Data

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.

Also Read This πŸ‘‰   Laravel 5.8 Autocomplete Textbox Fields Using jQuery

In this example we search the members data by name, age or many more.

MySQL – SEARCH Form with HTML and PHP

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>

Free Live Chat for Any Issue

search data from database in php with example

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();

?>

search form in php mysql

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>

search code in php

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();

?>

I hope you get an idea about search code in php to mysql database.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Rate this post