dtcyv3985 2016-10-08 05:34
浏览 86

将SQL更新查询转换为json请求

I have an sql update query like this; please note that I just have a string of the query like $query:

$query = 'UPDATE "public"."activity" SET "itemid" = 2, "itemname" = 'sssd' WHERE "id" = 19';

From this stringified query, I can get the set clause separately as:

$setClause = '"itemid" = 2, "itemname" = 'sssd'';

I need to convert this set clause to a json array to send in an ajax request like:

{
    "itemid": 2,
    "itemname": "sssd"
  }

So far I havent been able to figure this out; I looked online for a solution as well but couldn't find one. Any ideas please?

  • 写回答

1条回答 默认 最新

  • doufunuo4787 2016-10-08 08:26
    关注

    This did the job for me. Thanks to John Halbert for pointing it out in the comments.

    public function GetJsonFromUpdateSetClause($setClause){
            $sc2JSON1 = '{' . str_replace('=', ':', $setClause) . '}';
            $sc2JSON2 =  str_replace('\'', '\"', $sc2JSON1) ;
            $sc2JSON3 = stripcslashes($sc2JSON2);
            return $sc2JSON3;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏