Plss help guys !!
Posted: Sun Apr 12, 2015 12:38 pm
ive created my first php search function .. guys plss help i want to create a search function that display data from database into table format ..
here's my code for my php search function .. i want to improve my previous project
here's my code for my php search function .. i want to improve my previous project
Code: Select all
<?php
mysql_connect("localhost","root","") or die ("Could not connect to the server");
mysql_select_db("search_test") or die ("Could not find database !");
$output = '';
//collect
if (isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%$searchq%'") or die ("Could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There was no search result !';
}else{
while($row = mysql_fetch_array($query)){
$fname = $row['firstname'];
$lname = $row['lastname'];
$id = $row['id'];
$output .= '<div>'.$fname.' '.$lname.'</div>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
</head>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="Search for members" />
<input type="submit" value=">>" />
</form>
<?php print("$output"); ?>
<body>
</body>
</html>