Jump to content

making a php search


silverglade

Recommended Posts

Hi, I am trying to make a search based on information about a user in the database. Here is my code so far, notice any words like "and" or "the" in a multiword entry would mess it up. Any advice greatly appreciated. Ideally I would like a list of the matches by user name, and put the word "match" next to the matching data, because I used the OR statement it is going to bring back all of the user's info if one of the criteria matches. Any tutorials on the subject or keywords for google would be greatly appreciated. thanks.

 

here is the search code.

 

<?php 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$host		= " ;
$database 	= " ";
$username 	= " ";
$password 	= " ";
$tbl_name   = "users";


$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

if($conn)
{
mysql_select_db($database);
echo "connected to database!!";
} else {
	echo "failed to select database";
}




//include('bouncer.php');




//$currentUser = $_SESSION['myusername'];

if(isset($_POST['submit'])) 

{				 



$dob = mysql_real_escape_string( $_POST['dob']);
$gender = mysql_real_escape_string( $_POST['gender']);
$ethnic = mysql_real_escape_string( $_POST['ethnic']);

 	//THE SEARCH FUNCTION
$sql = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '%$dob%' 
OR gender LIKE '%$gender%'
OR race  LIKE 	'%$ethnic%' 
OR dob  LIKE '%$dob%' 
") or die(mysql_error());			 

  

$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() );

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["dob"];
    echo $row["gender"];
    echo $row["race"];
}





}//if(isset($_POST['submit']))
?>
<html>
<body>
<p>Login Successful</p>
<p> </p>

<form action="login_success7.php" method="post">
  <p>
  

    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
    <input type="text" name="gender" size="20" id="gender" />
  Gender <br />
  <input type="text" name="ethnic" size="20" id="ethnic" />
  Ethnicity
  <br />

    <input type="submit" name="submit" value=" search" />
    <input type="reset" value="Reset fields" />
  </p>
</form> 






  
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/246311-making-a-php-search/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.