0

table persons

name  |   details
------------------
mathew| tax,home,car,insurance
john  | job,tax,employ
neil  | tax,home,car,job
yancy | consultant,rent,family
lucy  | home,car,insurance

I want loop through this table and search with details then saved result to another table called persons1

name  |   names
------------------
mathew| neil,lucy,john
neil  | mathew,lucy,john
john  | mathew,lucy,neil

so far I coded something like below but not working

mysql_connect("localhost", "root", "pass"); 
mysql_select_db("database");
$query = "SELECT * FROM persons"; 
$result  = mysql_query($query); 
while($r = mysql_fetch_array($result)) 
{ 
    $exp = explode(",",$r["details"]);
    $sql = mysql_query('SELECT * FROM persons WHERE MATCH (tags) AGAINST ("+$exp[0]" "+$exp[1]" "+$exp[2]" IN BOOLEAN MODE)'); 
    $result = array(); 
    while($row = mysql_fetch_assoc($sql))
    {   
        array_push($result,$row['name']);
        $name = implode(",",$result);   
        mysql_query("INSERT INTO person_new (name,names) VALUES (\"".$r["name"]."\", \"".$name."\")");
    } 
}

IT is very sad that nobody can give an answer to my question about my code. instead of looking into my design I request you to look into my code and tell me where I made a mistake..i am doing something different than what it sees and this is why I request you to check my code...

2
  • Not working? Any error message? And change your title. Commented Feb 2, 2011 at 2:01
  • 3
    normalise your table design, its a terrible idea to have more than one piece of discreet data per cell. Commented Feb 2, 2011 at 2:07

2 Answers 2

4

Your problem would be better solved via database normalization.

Storing data like tax,home,car,insurance in a single column, then parsing it to search is a Very Bad Idea.

Sign up to request clarification or add additional context in comments.

5 Comments

I agree but I need it this way
@aron, No you don't need it this way. You have a design problem. The longer you ignore it, the more you'll paint yourself into a corner.
sorry I did not mean that...but this is for a new application which will redesign later but just now the format should be like this and that's why I am requesting..
I have enabled FULL TEXT SEARCH for this table
There's never time to do it right, but there's always time to do it over.
0

First of all, it'd be nice if you'd tell us what doesn't work.

Having said that, I suspect (at least one of) your error(s) is here:

'SELECT * FROM persons WHERE MATCH (tags) AGAINST ("+$exp[0]" "+$exp[1]" "+$exp[2]" IN BOOLEAN MODE)'
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This will literally give you the query AGAINST ("+$exp[0]" "+$exp[1]" "+$exp[2]" IN BOOLEAN MODE), which is likely not what you want. You need to concatenate the string, or use a double quoted string:

'SELECT ... AGAINST ("+' . $exp[0] . '" "+' . $exp[1] . '" "+' . $exp[2] . '" IN BOOLEAN MODE)'

or

"SELECT ... AGAINST (\"+$exp[0]\" \"+$exp[1]\" \"+$exp[2]\" IN BOOLEAN MODE)"

I'm with @Dolph though, this is not a good database structure, and if you're going to redesign it later anyway (careful with saying "later", that usually never happens), you should just do it now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.