Jump to content

Recommended Posts

What im trying to do is this:

 

#1. when someone goes to my site and types in a box " John Smith " it looks into the database under " fname, and lname" and displays all records with that name, or similar to that name. i also want them to be able to type jOhn smith. and it show up so it not be case sensitve.

 

im sure it would be a sql statement. im just trying to get this working.

Link to comment
https://forums.phpfreaks.com/topic/177984-solved-i-need-help-with-a-search/
Share on other sites

First of all break the input into chunks.

 

$data explode(" ",$string); will work

 

$data[0] now = 'john'

$data[1] now = 'smith'

 

Now.

 

Select * from database table where lname like '%$data[1]%' and fname like '%$data[0]%'

 

The % mean anything

 

so something%

 

means any record where the field starts with something.

 

good luck :)

Case sensitivity is based on the collation of your table.  The default collation, latin1, is case insensitive.

 

Alternatively, you can use a PHP function to set the input to lowercase before querying MySQL.  Be careful if you choose to follow Havok's suggestion as using dual wildcards, one on each end, may produce unexpected results.

Heres my temp code

 

if($submit) {

$searchc = explode(" ",$search);
$searchc[0]; 
    $searchc[1];
     $connect = mysql_connect("localhost","damgears_evil","damgears");
     mysql_select_db("damgears_evil");
     $query = mysql_query("SELECT * FROM titles WHERE fname LIKE '%$searchc[0]%'");

     while($row = mysql_fetch_assoc($query))
	{
		$dbid = $row['id'];
		$dbfname = $row['fname'];
		$dblname = $row['lname'];
		$dbkey = $row['key'];
		$dbskey = $row['skey'];
		$dbname = $row['sname'];
		$title = $row['title'];
		$dbstatus = $row['status'];
		$dbdate = $row['date'];
		$region = $row['region'];
		echo "$fname";
print "<center><b><a href=\"profile.php?fname=$dbfname&lname=$dblname\">$dbfname $dblname</a></b><br>";

	}


}

 

However...in the datbase there could be

 

John Smith <-- Same Person

John Smith <-- Same Person

John Sax

John Alverez <--

John Alverez <-- would be the same person

 

How do i get it to only state ( 1 ) of thenames and go on to the next one.

 

so it reads :

 

John Smith

John Sax

John Alverez

 

 

If the first and last name is all that you are interested in, the query would be:

SELECT DISTINCT fname, lname FROM titles WHERE ...

that will give you only rows where the combination is unique.

 

If you want to summarize some of the other data, you could do something like:

SELECT fname, lname, COUNT(title) as Articles FROM titles WHERE ... GROUP BY fname, lname

which will return one row for each unique combination of first and last name along with a count of the number of titles associated with the combination.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.