Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Searching SQL with PHP code
Old 02-13-2009, 11:01 AM Searching SQL with PHP code
Junior Talker

Posts: 1
Name: Simon Hardware
Trades: 0
Basically, I want one search box that will take the search string and cross reference each individual word with the cell data in the database and display the results.

I've got it working to a point (if I type Managing Director, it will find all Managing Director roles), however, what I'd like to happen is that by entering text into 1 form field, it will search all words against all the cells & return appropriate results.

For example; if I search, Managing Director London; I'd like it to return all MD roles in London. Now, the database is configured in such a way that the search will need to match Managing Director against 'job_title' & London against 'location'. Unfortunately, the code I'm using (see below) uses the whole string to compare against the database, so, it's searching the cells for 'Managing Director London', clearly this won't return any values for the reason above.

I don't want it to know whether it means 'managing' 'director london' or 'managing director' 'london', I want it to search each row with 'managing' and 'director' and 'london' and if those 3 appear anywhere in the row, it will be output to the results page.

This is the code I’m currently using;

<?php

$connection = mysql_connect("localhost","user","pass");
mysql_select_db("colvertgroup_co_uk_job",$connecti on) ;

$fields='`job_title` , `job_ref` , `job_desc` , `job_location` , `salary` , `job_type` , `key_words`';
$word = false;
if(!empty($_POST['search']))
{
$word=mysql_real_escape_string($_POST['search']);
$sql="SELECT $fields
FROM `tjob` WHERE concat($fields) LIKE '%$word%'";


if($word!=false)
{

$result=mysql_query($sql);
while ($row = mysql_fetch_row($result))
{
for ($i=0; $i<mysql_num_fields($result); $i++)
echo $row[$i] . " ";
echo "<br /><br>";
}
}
}
?>

Any Ideas?


simonhardware is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-13-2009, 12:03 PM Re: Searching SQL with PHP code
Skilled Talker

Posts: 79
Location: Devon, England
Trades: 0
You could split the search phrase into each word and put it into an array. Then build a mysql query string using a foreach loop....

PHP Code:
$wordAry explode(' ',$word);
$qStr '';
foreach (
$wordAry as $k=>$value){
    
$qStr = ($key == 0) ? ' concat($fields) LIKE "%'.$value.'%"' ' AND concat($fields) LIKE "%'.$value.'%"';

Then place $qStr in your query string.
__________________
Please add to my Talkupation if I was helpful. Thanks.


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
paaaaaaaaaa is offline
Reply With Quote
View Public Profile Visit paaaaaaaaaa's homepage!
 
Reply     « Reply to Searching SQL with PHP code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24818 seconds with 12 queries