简体   繁体   中英

How to extract Json data from Mysql using php

currently iam using the following code

  <?php

// simulates result of db query for categories
$categories = array();
$categories[] = array('id' => 1, 'parent_id' => 0, 'name' => 'root');
$categories[] = array('id' => 2, 'parent_id' => 1, 'name' => 'Compact Discs');
$categories[] = array('id' => 3, 'parent_id' => 1, 'name' => 'Concert Souvenirs');

// simulates result of db query for products
$products = array();
$products[] = array('id' => 1, 'category_id' => 2, 'sku' => 'CD001', 'price'=>15.00, 'name' => 'CD: Greatest Hits');
$products[] = array('id' => 2, 'category_id' => 2, 'sku' => 'CD002', 'price'=>15.00, 'name' => 'CD: Unplugged');
$products[] = array('id' => 3, 'category_id' => 2, 'sku' => 'CD003', 'price'=>15.00, 'name' => 'CD: World Tour');
$products[] = array('id' => 4, 'category_id' => 3, 'sku' => 'PD001', 'price'=>10.00, 'name' => 'Souvenir Pin');
$products[] = array('id' => 5, 'category_id' => 3, 'sku' => 'PD002', 'price'=>10.00, 'name' => 'Mug');
$products[] = array('id' => 6, 'category_id' => 3, 'sku' => 'PD003', 'price'=>20.00, 'name' => 'Hat');
$products[] = array('id' => 7, 'category_id' => 3, 'sku' => 'PD004', 'price'=>12.00, 'name' => 'Summer Tour Poster');
$products[] = array('id' => 8, 'category_id' => 3, 'sku' => 'PD005', 'price'=>5.00,  'name' => 'Concert Program');

// create the response
$response = array('categories' => $categories, 'products' => $products);

// display the json encoded response
echo json_encode($response);

an the output json result is like this

{"categories":[{"id":1,"parent_id":0,"name":"root"},{"id":2,"parent_id":1,"name":"Compact Discs"},{"id":3,"parent_id":1,"name":"Concert Souvenirs"}],"products":[{"id":1,"category_id":2,"sku":"CD001","price":15,"name":"CD: Greatest Hits"},{"id":2,"category_id":2,"sku":"CD002","price":15,"name":"CD: Unplugged"},{"id":3,"category_id":2,"sku":"CD003","price":15,"name":"CD: World Tour"},{"id":4,"category_id":3,"sku":"PD001","price":10,"name":"Souvenir Pin"},{"id":5,"category_id":3,"sku":"PD002","price":10,"name":"Mug"},{"id":6,"category_id":3,"sku":"PD003","price":20,"name":"Hat"},{"id":7,"category_id":3,"sku":"PD004","price":12,"name":"Summer Tour Poster"},{"id":8,"category_id":3,"sku":"PD005","price":5,"name":"Concert Program"}]}

But, i want the same Json outputfrom PHP using Mysql. My database structure is like,

category

id | parent_id | name |

Products

id | category_id | sku | price | name |

how should i write my php code so that I will get data from Mysql and take the json result like above ? Thanks in Advance.

I guess the clearest way would be to "convert" your sql result sets to the same structures you're already using - arrays. This way you won't have to change anything else - the rest of the algorithm will work as is. So simply fetch the result rows to same structure arrays as you used for simulation.

The way you approach it seems fine to me

Example snippet

$result = Array( 'Categories' => Array(), 'Products' => Array());

$stmt = $mysqli->prepare( 'SELECT id, parent_id, name FROM category' );
$stmt->bind_result( $id, $parent_id, $name );
$stmt->execute();
while( $stmt->fetch() ) {
  $result['Categories'][] = Array( 'id' => $id, 'parent_id' => $parent_id, 'name' => $name );
} 
$stmt->close();
//Do the same for the other one

$jsonstring = json_encode( $result );

From there you should be able to figure it out yourself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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