How to search data from table in php

———both HTML and php code in s1.php file—————-

<form method=”post” action=”s1.php?go” id=”searchform”>
<input type=”text” name=”name”>
<input type=”submit” name=”submit” value=”Search”>
</form>

 

<?php
if (isset($_POST[‘submit’]))
{
//connect to the database
$db = mysql_connect(“localhost”, “root”, “”) or die(‘I cannot connect to the database because: ‘ . mysql_error());
//-select the database to use
$mydb = mysql_select_db(“database_name”);if (isset($_GET[‘go’]))
{if (preg_match(“/[A-Z | a-z]+/”, $_POST[‘name’]))
{
$name = $_POST[‘name’];//-query the database table
$sql = “SELECT user_fname, user_lname FROM user WHERE user_fname LIKE ‘%” . $name . “%’ OR user_lname LIKE ‘%” . $name . “%'”;
//-run the query against the mysql query function
$result = mysql_query($sql);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result))
{
$FirstName = $row[‘user_fname’];
$LastName = $row[‘user_lname’];
echo $FirstName.$LastName;
}
}

}
}
?>

Please follow and like us: