尝试将查询结果转换为JSON格式并将其发送回我的手机(Android)

时间:2013-03-18 11:45:16

标签: php json

我正在尝试使用PHP在我的服务器上查询mySql数据库以下是我的代码:

   <?php

    require 'phtry.php';

    $message = 1 ; //$_POST["message"]; 

    $query = "SELECT `surname`,`firstname` FROM `users`";


    $query1 = "SELECT * FROM  `users` WHERE id = $message";

   if ($query_run = mysql_query($query1)){
      //echo 'Success.';
      while ($query_row = mysql_fetch_assoc($query_run)){

      $surname = $query_row['surname'];
      $firstname = $query_row['firstname'];


}


$out [] = $query_row;
print(json_encode($out));

}else{
    echo 'No Success';
}

然而声明打印(json_encode($ out));给我输出[false]。谁能指引我朝着正确的方向前进

1 个答案:

答案 0 :(得分:1)

在循环中放入$ out

if ($query_run = mysql_query($query1)){
 //echo 'Success.';
 while ($query_row = mysql_fetch_assoc($query_run)){

 $surname = $query_row['surname'];
 $firstname = $query_row['firstname'];
 $out[] = $query_row;

}
print(json_encode($out));
}else{
 echo 'No Success';
}