简体   繁体   English

如何使用php将mysql数据提取到json中

[英]how to extract mysql data into json using php

i have retrieved mysql data from one table in json using the following script 我已经使用以下脚本从json中的一个表中检索了mysql数据

$table_first = 'abc';
$query = "SELECT * FROM $table_first";
$resouter = mysql_query($query, $conn);

$set = array();

$total_records = mysql_numrows($resouter);
if($total_records >= 1){

  while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){
    $set[] = $link;
  }
}

echo json_encode($set);

how can i retrieved data from two other tables in which there is a foreign key of this table in both of those tables. 我如何从另外两个表中检索数据,在两个表中都有该表的外键。 OR simply how can i retrieved data from 3 mysql tables in php. 或者干脆我怎么能从PHP中的3个mysql表中检索数据。

I believe the best way to go here is using a JOIN or just something like this: 我相信最好的方法是使用JOIN或类似这样的方法:

$sql = "SELECT 
            tabl1.*, table2.*, tabl3.* FROM table1, table2, table3 
        WHERE 
            table1.fk1 = table2.id AND
            table1.fk2 = table2.id";

//Do the whole selection process...

If you make the queries separately, you'll be forcing 3 queries onto your database and will end in a performance hit that you dont need. 如果您分别进行查询,则将对数据库强制执行3个查询,最终会导致不必要的性能下降。 So, the idea is load all the data from the DB using joins or similar that and then encode the results. 因此,这个想法是使用联接或类似方法从数据库中加载所有数据,然后对结果进行编码。 Is faster and you'll leave the merging work to MySQL 更快,将合并工作留给MySQL

Hope I can help 希望我能帮忙

I believe your code roughly will look like this: 我相信您的代码大致如下所示:

$query = "SELECT 
        A.column1 AS First_1
        A.column2 AS First_2
        B.column2 AS Second
        C.column3 AS Third
        FROM table1 A, table2 B, table3 C 
    WHERE 
        A.fk1 = B.id AND
        B.fk2 = C.id";

where a column is a relevant record you want to show. 列是要显示的相关记录。 Meanwhile, AS will act as a key name in JSON. 同时,AS将充当JSON中的键名。

You can get all data firstly. 您可以先获取所有数据。 Then merge the data array. 然后合并数据数组。 Finally use json_encode to change the data format. 最后使用json_encode更改数据格式。

在这两个表中都有一个外键,因此您可以使用“ join”从其他表中检索值。

Suppose that there are two tables as State(st_id,st_name) and City(ct_id,ct_name,state_id). 假设有两个表,分别是State(st_id,st_name)和City(ct_id,ct_name,state_id)。 Now, primary key are st_id & ct_id respectively of tables State & City. 现在,主键分别是表State和City的st_id和ct_id。

Connection between this two table can be establish by joining State.st_id and City.state_id. 这两个表之间的连接可以通过连接State.st_id和City.state_id来建立。

Now, coming to your problem to retrieve data from two table State & City, we can make sql query like following, 现在,要解决您要从两个表State&City检索数据的问题,我们可以像下面这样进行sql查询,

$sql="select s.*, c.* from State s, City c
       where s.st_id=c.state_id ";

Using above query you can fetch data from database and convert into json format and can send it to android system. 使用上面的查询,您可以从数据库中获取数据并转换为json格式,然后将其发送到android系统。 here is a good article http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/ . 这是一篇很好的文章http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/ i hope you like it. 我希望你喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM