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 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面