doucong7963 2017-06-17 13:06
浏览 48
已采纳

使用PHP脚本将数据库列的内容复制到另一列

I have a site that holds many posts in the database. Currently, my table structure looks like this:

|-------|-------|-------------------|
|   id  |   url |   title           |
|-------|-------|-------------------|
|   1   |       |   Hello World     |
|   2   |       |   Hello World's   |
|   3   |       |   Hello & World   |
|-------|-------|-------------------|

In the table above looks empty url column. Yes, that column I just added, so there is nothing there. While in the title column, there are different post titles.

I have a problem with the title field, because there are some unsupported characters for the URL structure. For example http://google.com/?url=Hello World's So does the string & does not work there.

I plan to add a new column to serve as the url address. So like this my new table will be:

|-------|-------------------|-------------------|
|   id  |       url         |   title           |
|-------|-------------------|-------------------|
|   1   |   hello_world     |   Hello World     |
|   2   |   hello_worlds    |   Hello World's   |
|   3   |   hello_and_world |   Hello & World   |
|-------|-------------------|-------------------|

The addition of this url can be done manually via PHPMyAdmin, but my list of posts there are thousands of them. It is impossible to do it one by one. Is there an instant way to duplicate all data in the column title to the url column using PHP script and change (spaces) to (_), remove quotes, and change the sign (&) to (and)?

Please do not give me advice to use ID as url address ?url=1, ?url=2, ?url=3, because I want to use title as my post address. thanks.

  • 写回答

1条回答 默认 最新

  • doufei2662 2017-06-17 13:08
    关注

    You can do this with a bunch of replaces. You can do:

    update t
        set url = replace(replace(replace(title, ' ', '_'), '&', 'and'), '''', '');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line