简体   繁体   中英

How to retrive php data from mysql and convert it to json?

My mind got stuck when i try to develope this: i have a table in my database called "article" whit two column, "name" and "price". How can i extract all rows from my table and echo all column in JSON? i really can't understand how to convert result in JSON. My mind it's stuck like never before. i need to echo something like this:

{"items": {
    "items":[
        {"name": "firstitemname", 
         "price": "5"
        },
        {"name": "secondone", 
         "years": "3"
        }],
}}

Please help me fixing my buggy code!

<?php

$query = mysql_query("SELECT * FROM itemlist");
$nameitem = array();
$itemprice = array();
while($row = mysql_fetch_array($query)){
array_push($nameitem , $row['nome']);
array_push($itemprice, $row['pix']);
}

?>

You would simply edit your PHP as follows.

<?php

$query = mysql_query("SELECT * FROM itemlist");
$items = array();
while($row = mysql_fetch_array($query)){
$items[] = array('name' => $row['nome'], 'price' => $row['pix']);
}

echo json_encode(array('items'=>$items));

?>    

http://php.net/manual/en/function.json-encode.php

JSON is super easy to deal with in PHP.

<?php
$result = mysql_query("select * from item_list");
$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
print json_encode($rows);
?>

If you're using PHP 5.2 or greater, you can use the json_encode function to do exactly what you're trying to do: http://www.php.net/manual/en/function.json-encode.php

For your code, you should be able to do something like this:

$query = mysql_query("SELECT * FROM itemlist");
$json_output = array();
while($row = mysql_fetch_assoc($query)){
    $json_output[] = json_encode($row);
}

Here, $json_output will contain an array of strings with the json encoded string of each row as each array element. You can output these as you please.

Convert Data table to json with following code:

echo(json_encode($array));  

for example ( select data from mysql and convert to json ):

 public function SELECT($tableName,$conditions){

      $connection = mysqli_connect($hostname, $userName, $password,$dbName);
      try {

        if (!$connection)
            die("Connection failed: " . $connection->connect_error);
        else
        {
            $qry = "";
            if(!$this->IsNullOrEmptyString($conditions))
               $qry = "SELECT * FROM `".$tableName."` WHERE ".$conditions;
            else
               $qry = "SELECT * FROM `".$tableName."`";

            $result = mysqli_query( $connection, $qry);
            if($result) {
                $emparray = array();
                while($row =mysqli_fetch_assoc($result))
                    $emparray[] = $row;

                echo(json_encode($emparray));           
            }
            else
                echo(mysqli_error($connection));       
            } 
            mysqli_close($connection); 
      } catch(Exception $ex) {
          mysqli_close($connection);
          echo($ex->getMessage());
      }  
 }

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