Search...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Search...

Post by ursl40 »

Hy!

I have a 'search function' that searches through all tables in my DB (everywhere), through table users (users) or through table cars (cars) - (in brackets are three options of my dropdown)...

How can I search name of the user and surname at the same time, so I have:

Search: User-name_user-surname (users)

After space, 'query' ignores other searching values.

Thanks!
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Search...

Post by klevis miho »

Do a query like this:
"SELECT * FROM users WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $lastname ."%'"
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Search...

Post by ursl40 »

This isn't working for me...

I have:
$data = mysql_query("SELECT * FROM users WHERE name LIKE '% ". $find ." %' OR surname LIKE '% " . $find ." %'");
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Search...

Post by ursl40 »

Can someone help me please? Thanks!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Search...

Post by s.dot »

You have a space after % in your query. and before the %

Change it to LIKE '%". $find ."%'
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Search...

Post by ursl40 »

No, I don't have spaces before and after % in my code, I must have misstyped myself here...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Search...

Post by s.dot »

Do a var_dump($find); before your query. This is to check the value of $find before the query is executed.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Search...

Post by ursl40 »

For john miller I get:

string(11) "john miller"

but still nothing was found...

So, I have field name and field surname in table users, I search with keywords 'name surname' and nothing is found.

I've also tried this:
$lines=split(" ", $find); // $find is variable for input
for ($i=0;$i<count($lines);$i++) {
$data=mysql_query("SELECT * FROM users WHERE name LIKE '%".$find."%' OR surname LIKE '%".$find."%'"); }

No success...
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Search...

Post by mikosiko »

ursl40 wrote: $data = mysql_query("SELECT * FROM users WHERE name LIKE '% ". $find ." %' OR surname LIKE '% " . $find ." %'");
try...

Code: Select all

$data = mysql_query("SELECT * FROM users WHERE CONCAT_WS( ' ', name, surname)  LIKE '%". $find ."%'")
pay attention that is one space here : ' '
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Search...

Post by Darhazer »

ursl40 I would recommend you to try using Solr
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Search...

Post by ursl40 »

Can I use this with PHP pages, how? Thanks!
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Search...

Post by ursl40 »

mikosiko wrote:
ursl40 wrote: $data = mysql_query("SELECT * FROM users WHERE name LIKE '% ". $find ." %' OR surname LIKE '% " . $find ." %'");
try...

Code: Select all

$data = mysql_query("SELECT * FROM users WHERE CONCAT_WS( ' ', name, surname)  LIKE '%". $find ."%'")
pay attention that is one space here : ' '
With Your suggestion, it throws me a warning:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ...x.php on line y
Post Reply