php search function code example
Example 1: array_search in php
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
$key = array_search('red', $array);
?>
Example 2: search function using php for database entries
$result = mysqli_query($con, "SELECT * FROM employees
WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'");
while ($row = mysqli_fetch_array($result))
{
echo $row['first_name'] . " " . $row['last_name'];
echo "<br>";
}
mysqli_close($con);
?>
Example 3: array_search in php
<?php
$a = [
'Canceled' => 1,
'Traded / Filled'=> 2,
'(Not used currently)'=> 3,
'Transit'=> 4,
'Rejected'=> 5,
'Pending'=> 6,
];
echo array_search("5",$a);
?>
Example 4: search function in php
<?php
mysql_connect("localhost","root","") or die("could not connect");
mysql_select_db("kkp") or die("could not find db!");
$output ='';
if (isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM personal_info WHERE FirstName LIKE '%$searchq%' OR SurName LIKE '%$searchq%'") or die("could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There was no search results!';
}else{
while($row = mysql_fetch_array($query)){
$fname = $row['FirstName'];
$lname = $row['SurName'];
$id = $row['id'];
$output .= '<div>'.$fname.''.$lname.'</div>';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="post" action="search.php"></form>
<input type="text" name="search" placeholder="Search for student">
<input type="submit" value="Submit">
</body>
</html>
<?php print("$output");?>