简体   繁体   中英

print this query out put in json format to use in php webservices

i have written this query in php file to get data from database ,it is working fine and getting required data . but how to print the retrieved data in json fromat for using web services

$query  = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
   $subject = mysqli_fetch_assoc($result);
 print_r($subject);

what code has to be done please help me.

Have you tried json_encode? Like:

$query  = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);

echo json_encode($subject);

For more information you can check: PHP Manual

try this :

$query  = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);

header('Content-Type: application/json');
echo json_encode($subject);

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