dongxianglun5163 2014-04-26 12:18
浏览 71
已采纳

将PHP-SQL Result变量转换为Javascript对象

I am making a simple web app, in one part of it, the result of SQL query is to be passed from PHP to JavaScript with AJAX.

This is the SQL Query:

$meta_query = mysql_fetch_row(mysql_query("SELECT * from meta WHERE user_id='$user_id'"));

This is how I pass it to JavaScript var_dump($meta_query);

This is what I am getting at JavaScript (as a string):

array(30) {[0]=>string(1) "3"["id"]=>string(1) "3"[1]=>string(2) "14"["user_id"]=>string(2) "14"[2]=>string(10) "29-06-2014"["date"]=>string(10) "29-06-2014"[3]=>string(1) "0"["present"]=>string(1) "0"[4]=>string(1) "0"["future"]=>string(1) "0"}

How, how do I convert this into a proper JavaScript object so that the output is something like:

{"id":"3","user_id":"14","date":"29-06-2014","Present":0,"Future":0}

How do I convert data from the first format to the second in JavaScript? Or should I do something else entirely in PHP to get the data out in some other format? Or should I convert the data to the required format somehow in PHP?

  • 写回答

2条回答 默认 最新

  • doucan1979 2014-04-26 12:22
    关注

    You might want to use

    echo json_encode($meta_query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败