<?php include("dbinfo.inc.php");  mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$operation = $_GET['operation'];
$brand = $_GET['brand']; if($brand == 'hp'){ $query = "SELECT * FROM laptop WHERE name like '%" . $brand . "%'"; mysql_query("SET NAMES UTF8");
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();

$i = 0;
while ($i < $num) { 

$price = mysql_result($result,$i,"price");

$weight = mysql_result($result,$i,"weight");

        $json = '{"laptops":{"price": "$price","weight": "$weight"}}';

$json_string = json_encode($json); 

$obj =  json_decode($json_string);   

echo "$obj";

$i++;}
} else {

echo "error"; }?>
我现在想从数据库查询得到hp的price 和weight 的值,因为数据库中有很多,我想把得到的值,存成json的形式,然后编码和解码,在输出回来。有没有高手啊,用过php操作从数据库返回的json对象啊。先谢谢了啊。

解决方案 »

  1.   

    我先的问题是,第一,在$json 中$price的值都不到,第二,现在输出来的形式是{"laptops":{"price": "$price","weight": "$weight"}}{"laptops":{"price": "$price","weight": "$weight"}},不是一个json 的形式啊。
      

  2.   

     echo "$obj";
    这个就是解释后的 json 串了。就是你一楼的这个形式了。具体形式,得看你 json 前的是什么样子了。
      

  3.   


    我的现在这个流程是没有问题的,就是从数据库中得到数据后,我怎么写成json的形式的问题。
      

  4.   

    你写的格式有问题你自己造一个多维数组,echo json_encode($arr);看看格式
      

  5.   

    <?php
    include("dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");$operation = $_GET['operation'];
    $brand = $_GET['brand'];
    if($brand == 'hp'){
    $query = "SELECT * FROM laptop WHERE name like '%" . $brand . "%'"; mysql_query("SET NAMES UTF8");
    $result = mysql_query($query);
    $num = mysql_num_rows($result);
    mysql_close(); $i = 0;
    while ($i < $num) {
    $price = mysql_result($result,$i,"price");
    $weight = mysql_result($result,$i,"weight");
    $json = '{"laptops":{"price": "$price","weight": "$weight"}}';
    $json_string .= json_encode($json); //连续输出
    echo "$json_string";
    $i++;
    }
    } else {
    echo "error";
    }
    ?>
      

  6.   


    在$json 中$price的值都不到,怎么回事啊。
      

  7.   


    连续输出的结果,还是{"laptops":{"price": "$price","weight": "$weight"}}{"laptops":{"price": "$price","weight": "$weight"}}的形式啊,这种形式并不是一个json的形式啊。
      

  8.   

    1 json_encode 你只需要传一个数组就可以了 。
    2 json_encode 只支持utf-8
      

  9.   

    还在吗?在$json 中$price的值都不到,怎么回事啊?
      

  10.   

    代码有误,单引号中的变量不会取值
    $json = "{\"laptops\":{\"price\": \"$price\",\"weight\": \"$weight\"}}";
    这是 json_encode 的结果你可以这样写:
    $p->laptops->price = $price;
    $p->laptops->weight = $weight;
    echo json_encode($p);另外,你的数据库操作语句有误:
    $result = mysql_query($query);
    $num = mysql_num_rows($result);
    mysql_close();
    到数据库的连接都关闭了,后面怎么取值?
      

  11.   

    关闭了,可以取到值的,不是mysql_free_result
      

  12.   

    是吗?很久没有用mysql了,记得查询结果是缓存在mysql,而不是php的
      

  13.   

    从数据库得到的数据,先用数组的方式操作,最后得到你要的结果后,直接json_encode($array);就可以了,它返回的就是一个json格式的字符串。
    你在JS里可以直接得到/使用它。
    var jsondata=<?php echo json_encode($array);?>;你的代码:
    $json = '{"laptops":{"price": "$price","weight": "$weight"}}';
    $json_string = json_encode($json); 
    $obj =  json_decode($json_string);
    这不是画蛇添足吗?
      

  14.   

    json_decode($json_string);//返回一个对象
    (array)json_decode($json_string);和json_decode($json_string,true);//返回一个数组