Page 1 of 1

Plss help guys !!

Posted: Sun Apr 12, 2015 12:38 pm
by joshuadelfin19
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

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>

Re: Plss help guys !!

Posted: Sun Apr 12, 2015 6:29 pm
by Celauran
The first step toward improving it is getting rid of the deprecated mysql_ calls and replacing them with PDO prepared statements. If you want to display the results in a table, just iterate over the results like you're doing and replace the div with the appropriate table cells.