duanjue9296 2013-06-10 22:13
浏览 30
已采纳

编码SQL PDO SQL结果

I am using a PHP web service to get results from an sql query within one of the tables has quotes and when I use the query below it the result shows this table as null. Now I had this issue already and what I had to do was encode the array then with utf8_encode. However that doesn't seem to work now. How can I now encode my results to show the table in my json results.

My code is below

$id = 14;
include("file.php");
$query = "SELECT * FROM table WHERE table_id='$id'";
$sth = $dbc->query($query);
$result = $sth->fetchAll();
$result = utf8_encode($result);
/* output in necessary format */

header('Content-type: application/json; charset=utf-8');
echo $_GET['onJSONPLoad'];
echo "(" . json_encode($result) . ")";
  • 写回答

1条回答 默认 最新

  • dongzanghong4379 2013-06-11 09:50
    关注

    Fixed the issue it seems that I had to set the PDO itself to utf8. The code below worked

    $dbc = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目