从PHP和mySQL返回整数到Android

时间:2012-10-30 00:34:36

标签: php android json

我有一个mySQL数据库在线。我可以更新分数,它工作正常。现在我想向Android返回一个行号,但我不能这样做。我的php文件:

 <?php

$name = $_REQUEST['jugador'];
$score = $_REQUEST['score'];
$moles = $_REQUEST['topos']; 

$con = mysql_connect("hots","db","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a5385197_scores", $con);

mysql_query("INSERT INTO scores VALUES (null, '$name', $score, $moles)");

$result = mysql_query("SELECT COUNT(1) FROM scores WHERE score >= $score");

echo json_encode($result);

mysql_close($con);
 ?>

插入工作正常,Select的句子返回正确的值,但我无法发送到Android。我的Android代码回复:

        try {
             BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
             }
             is.close();

             result = sb.toString();
             Log.i("miTraza","Recive: "+result);
        }catch(Exception e){
                Log.e("miError", "Error converting result "+e.toString());
        }

结果我收到错误:

10-30 01:13:29.445: I/miTraza(29304): Recive: <br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />
10-30 01:13:29.445: I/miTraza(29304): <b>Warning</b>:  [json] (php_json_encode) type is unsupported, encoded as null in <b>/home/a5385197/public_html/try.php</b> on line <b>21</b><br />
10-30 01:13:29.445: I/miTraza(29304): <br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><div align='center'><a href='http://www.000webhost.com/'><font face='Arial' size='1' color='#000000'>Free Web Hosting</font></a></div></td></tr></table>null
10-30 01:13:29.445: I/miTraza(29304): <!-- Hosting24 Analytics Code -->
10-30 01:13:29.445: I/miTraza(29304): <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
10-30 01:13:29.445: I/miTraza(29304): <!-- End Of Analytics Code -->

我从PHP开始,所以很容易出错。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

echo json_encode(mysql_fetch_array($result));