English 中文(简体)
有没有办法让PHP转换 sql结果直接到JSON?
原标题:Is there a way to have PHP convert sql results straight to JSON?

我很好奇我是否必须自己滚动, 或者是否有一个预制的 PHP SQL 库, 我俯视它, 这样我就可以通过一个 sql 选择的查询, 并因此给我一个 JSON 对象( 或字符串 ) 。

在假代码 我想要的是这个:

$startIndex = 0;
$myJSON = magicSqlToJSON("select first_name, last_name, phone,  (select count(id) from users) as total from users limit $startIndex, 2");

我的JSON现在是:

{"users":[
   {"first_name":"Peter", "last_name":"O Tool","phone":"1234567890","total":"100"},
   {"first_name":"Gary", "last_name":"Shandling","phone":"1234567890","total":"100"}
]}

我知道自己写这个不会花很长时间, 但我觉得这太常见了, 以至于它已经不存在了。

最佳回答

PHP 中不存在本地存在的 single 函数

然而,您可以使用 mysqli_fetch_array () (例如) 和 json_encode () json_encode () 的组合,快速完成这项工作。 您可能不得不稍微调整父格式( 即“ 用户 之下) 。

问题回答

PHP

为此,PHP具有 json_encode () json_decode () 功能,但您需要手动用 foreach 或类似的 来绕过您的数据。

MySQL

您可以使用用户定义函数, 如< a href=> http://www. mysqludf.org/lib_mysqludf_json/index.php" rel=“ no follow” >lib_mysqludf_json , 这样您就可以从这样的查询中返回 JSON 阵列 :

select json_array(
           customer_id
       ,   first_name
       ,   last_name
       ,   last_update
       ) as customer
from   customer 
where  customer_id =1;

得出这一结果:

+------------------------------------------+
| customer                                 |
+------------------------------------------+
| [1,"MARY","SMITH","2006-02-15 04:57:20"] |
+------------------------------------------+

UDF 中还有一个 json_object 函数,应该能非常接近地代表您问题中的样本。

no 直接函数,但你可以做其他事情:

结果来自一个阵列,所以只是调用

json_encode( array); - & gt; http://php. net/ manual/ en/ office.json- encode. php

在它上,就在这上面

示例:

$query = "select first_name, last_name, phone,  (select count(id) from users) as total from users limit $startIndex, 2";
$result = mysql_query($query) or die(mysql_error());

echo json_encode(mysql_fetch_array($result));

我假设你用PDO

echo json_encode($pdo->exec($sql)->fetchAll());

否则,一般模式是:

$handle = execute_query($sql);
$rows = array();
while ($row = get_row_assoc($handle)) {
    $rows[] = $row;
}
echo json_encode($rows);




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...