【问题标题】:I want to know good syntax for mysql pdo json我想知道 mysql pdo json 的好语法
【发布时间】:2018-04-07 07:00:02
【问题描述】:

我还没有找到使用json_set 更新mysql 上的表的好语法。

你能给我一些帮助吗?我想更新并了解此查询的良好语法。

public function mkjson()
{
    $friends = '"pachou", "eric", "francis"';
    $tagfriendsjson = json_encode($friends);

    try
    {
        include('connect.ini');
    }
    catch (Exception $e)
    {
        die('Erreur : '.$e->getMessage());
    }

    $ver = 'SELECT * FROM tagusers WHERE tag_users_id="'.$_SESSION['tagusersid'].'"';
    $ask = $bdd->query($ver);
    $ans = $ask->fetch();

    if (empty($ans['tag_friends']))
    {
        $req = $bdd->prepare('UPDATE tagusers SET = {"1": "pachou", "2": "eric", "3":  "francis", "4": "Henry"} WHERE tag_users_id="'.$_SESSION['tagusersid'].'" ');
        $req->execute();
    }
    else
    {

    }
    return true;
}

:未捕获的 PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 /var/www/tagmeyou.com/php/ 的第 1 行的 '= {"4": "Henry"} WHERE tag_users_id="2"' 附近使用正确的语法class.Friendlist.php:34 堆栈跟踪:#0 /var/www/tagmeyou.com/php/class.Friendlist.php(34): PDOStatement->execute() #1 /var/www/tagmeyou.com/fr /vues/body/inputtag.php(23): 好友列表->mkjson() #2 /var/www/tagmeyou.com/fr/vues/body/bodytagscreen.php(10): include('/var/www/ tagmey...') #3 /var/www/tagmeyou.com/fr/vues/tagmeyou.php(34): include('/var/www/tagmey...') #4 {main} 抛出

【问题讨论】:

  • 我真的不建议使用json作为字段值,太乱了:S

标签: mysql json pdo


【解决方案1】:

这个查询有两个错误:

UPDATE tagusers SET = {"1": "pachou", "2": "eric", "3":  "francis", "4": "Henry"} WHERE tag_users_id="'.$_SESSION['tagusersid'].'"

首先,没有设置列名(在SET= 之间)。其次,JSON 应该是单引号。尝试将该行更改为

    $req = $bdd->prepare('UPDATE tagusers SET colname = \'"1": "pachou", "2": "eric", "3":  "francis", "4": "Henry"}\' WHERE tag_users_id="'.$_SESSION['tagusersid'].'" ');

colname 是您要在其中存储 JSON 的列。

【讨论】:

  • 如果它解决了您的问题,最好将其标记为已接受,以便其他搜索此问题的用户可以找到它。
猜你喜欢
  • 1970-01-01
  • 2018-08-30
  • 2012-12-24
  • 2016-09-10
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
相关资源
最近更新 更多