Mysql PHP Sample PHP Mysql search program Sample Programs

Search and Retrieve Data from MySQL DB Using PHP


Below is the Simple PHP and MYSQL Search Program.

Use below database queries:

CREATE TABLE IF NOT EXISTS user (id int(10) NOT NULL AUTO_INCREMENT,Name varchar(20) NOT NULL,PRIMARY KEY (id));

INSERT INTO user (id, Name) VALUES (‘1’, ‘vicky’), (‘2’, ‘vivek’);

Below is the sample program:










$search = $_POST['search'];
$search = trim(strip_tags($search));
if($search) {

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'vivek';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db("general");

$sql = "SELECT * FROM user WHERE Name LIKE '%$search%' or Name LIKE '%$search%' ";
$retval = mysql_query($sql, $conn);

while($row = mysql_fetch_array($retval)) {

echo $row["Name"] . "
";

}

}
?>