dounao2829 2016-05-20 08:33
浏览 31

json_encode在php中的数组

I have a problem with json encode of an array of variables $name0 and $name1 If I use a simple echo $name0 and echo $name, I get the correct result for both variables. But if I put $name0 and $name1 in an array $arr and then encode the array with echo json_encode($arr), in my browser I get the value of $name0. But the value of $name1 is "null". How can I get both values right ($name0 and $name1)? Thank you for your help. I am really stuck.

PHP

$sql= "SELECT * FROM  names WHERE userId= '$userId'  AND  number = 0";
    $rs =mysql_query($sql, $connexionLogin) or die(mysql_error());
    $row_rs = mysql_fetch_assoc($rs);
    $total_rs = mysql_num_rows($rs);
    $totalName0 = $total_rs;
    if ($total_rs>0) {
        $name0 = $row_rs['name'];
    }else  if ($total_rs==0) {
        $name0 = "empty";
    }
    echo $name0 //returns "This is the first name"
    $sql= "SELECT * FROM  names WHERE userId = '$userId'  AND number= 1";
    $rs =mysql_query($sql, $connexionLogin) or die(mysql_error());
    $row_rs = mysql_fetch_assoc($rs);
    $total_rs = mysql_num_rows($rs);
    $totalName1 = $total_rs;
    if ($total_rs!=0) {
        $name1 = $row_rs['nom'];
    }else  if ($total_rs==0) {
        $name1 = "empty";
    }
   echo $name1; // returns "This is the second name"


    $arr = array(
        'name0' => $name0,
        'name1' => $name1
    );
    echo json_encode($arr); //returns 

This is the first nameThis is the second name{"name0":"This is the first name","name1":null}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。