dtcu5885 2013-09-23 12:59
浏览 49
已采纳

使用DBO连接插入或更新带有json值的MySQL表失败

Data is a json-encoded. It seems to always fail with errors as if the string has not been escaped properly even though I've tried using both quote() as the documentation suggests.. and also mysql_real_escape_string() but nothing seems to work :S

If I change $data to 'apa' for example instead of my json I get:

PDO::errorInfo(): Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''\'apa\'')' at line 1 )

-

$data = json_encode($jsonStoreArr, JSON_HEX_APOS | JSON_HEX_QUOT);
$stmt = $pdo->prepare("INSERT INTO _mytablename (movie_id, cached_data) VALUES (:id, :data) ON DUPLICATE KEY UPDATE cached_data = values(:data)");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':data', $data, PDO::PARAM_STR);
$stmt->execute();   
if ($stmt->errorCode() !== '00000') {
     print_r($stmt->errorInfo());
}
  • 写回答

1条回答 默认 最新

  • dongtan5811 2013-09-23 13:21
    关注

    I urlencoded urls in the json string and then simply did this (below), now it works!

    $data = json_encode($jsonStoreArr);
    $stmt = $pdo->prepare("INSERT INTO _mytablename (movie_id, cached_data) VALUES (:id, :data) ON DUPLICATE KEY UPDATE cached_data = :data");
    $stmt->bindParam(':data', $data, PDO::PARAM_STR);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料