dougaimian1143
dougaimian1143
2013-08-19 08:45

使用表A的内容更新表B?

已采纳

I have 2 tables, Table A:

sid, phone, ts

And Table B:

id, phone, sid(NULL), ts

I have written a PHP script to connect, query and read in the data, but am not sure how to edit the data.

What I essentially want to do is match tableB phone with tableA phone and if match then update tableB column sid which is NULL with tableA sid.

I am sure this can be done with while and foreach loop but am not exactly sure how.

Can anyone please help?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douxishai8552 douxishai8552 8年前

    If you need to automatically update the db, then you should be using CRON job.

    Step 1 : Copy paste the following in a separate php file say, cron.php. Add the db connection part also. :

    $query_table1 = "SELECT * FROM table1";
    $result_table1= msyql_query($query_table1);
    while($row_table1 = mysql_fetch_array($result_table1)){
        $phone1 = $row_table1['phone'];
        $sid1 = $row_table1['sid'];
    
        $query_table2 =  "UPDATE table2 SET sid=$sid1 WHERE phone=$phone1";
        $result_table2= msyql_query($query_table2);
    }
    

    Step 2 : Go to your host control panel and go to cron. In that add a new cron job.

    If you need to execute the file every 1 min then, this would help the cause:

    * * * * * <command> #Runs every minute

    The basic syntax would be time /path/to/command arg1 arg2

    Link to Cpanel docs

    Cheers

    点赞 评论 复制链接分享
  • dongqu5650 dongqu5650 8年前

    You have to run this query:

    update tableA, tableB set tableB.sid = tableA.sid where 
                     tableB.phone = tableA.phone
    
    点赞 评论 复制链接分享

为你推荐