Reputation: 33
I want to search a database. I want to search only by name and then post results from that one name only..If similar names come up I want them to be able to choose right name.. Once name is chosen I want to display results. But I want the results to be styled on the page... Here is my code now.. Need to know how to style the results...and how to query results with similar names.
<?php
mysql_connect("local", "dbname", "pass") or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
root - your username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("db") or die(mysql_error());
/* tutorial_search is the name of database we've created */
?>
<?php
$query = $_GET['query'];
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM member
WHERE (`Name` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<p><h3>".$results['Name']."</h3>".$results['Telephone']." <br>".$results['ProviderID']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
Upvotes: 0
Views: 2928
Reputation: 960
try this using dropdown box so that you can find exact name related values...and next time please use mysqli()...because mysql() is deprecated
<table width="100%" align="center">
<td valign="top" align="center" style="">
<form name="form1" id="form1" method="post" action="search.php"> //change here
<tr>
<td >
Select Name :
</td>
<td>
<input type="text" name="query" />
</td>
</tr>
<tr><td>
<input type="submit" name="submit" id="submit" value="FILTER" />
</td>
</tr>
</form>
</table>
this will be your search.php
<table align="center" border="1" cellpadding="0" cellspacing="0" width="100%" bordercolor="#804000">
<?php
echo '<tr>
<td><b> Name</b></td>
<td><b> Telephone</b></td>
<td><b> ProviderID</b></td>
</tr>';
$select="SELECT * FROM member WHERE name='".$_POST['query']."'"; // here input box name
$query=mysql_query($select) or die($select);
$numrow=mysql_num_rows($query);
if($numrow != 0)
{
while($row=mysql_fetch_array($query))
{
?><tr align="left" >
<td><b> <?php echo $row['name']; ?></b></td>
<td><b> <?php echo $row['telephone']; ?></b></td>
<td ><b> <?php echo $row['ProviderID']; ?></b></td>
</tr><?php } } else { echo '<table ><tr align="center" style="font-size:16px;line-height:150px;color:#FF0000;">
<font color="#FF0000" >NO RECORDS FOUND</font>
</tr></table>'; } ?>
</table>
Upvotes: 1