doucheng4094 2011-02-02 01:59
浏览 46
已采纳

如何在MySQL中循环使用PHP循环?

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条回答 默认 最新

  • dongmi5177 2011-02-02 03:45
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!