php json_encode mysql结果

时间:2010-08-25 06:38:11

标签: php mysql json

我有一个简单的mysql_query,我想在json中对results(title= > $title, price => $price etc)进行编码。

$sql = mysql_query("SELECT * FROM item_details WHERE posting_id='$item_number'");

 while($row = mysql_fetch_array($sql))
{
   $title = base64_decode($row['title']);
   $price = $row['price'];
   $seller_user = $row['user'];
}

1 个答案:

答案 0 :(得分:18)

$sql = mysql_query("SELECT * FROM item_details WHERE posting_id='$item_number'");
$results = array();
while($row = mysql_fetch_array($sql))
{
   $results[] = array(
      'title' => base64_decode($row['title']),
      'price' => $row['price'],
      'seller_user' => $row['user']
   );
}
$json = json_encode($results);