doufei8250 2016-03-14 15:22
浏览 23

使用php代码连接MySQL时如何检查db是否为空

I'm currently working on an Android application. It is connected to the free database provided by 000webhost. I have a table created in the database. The data storing and retrieving process work fine if there are data stored in the table. But if there are no data stored in the table, the application cannot even be run. So I guess one way to sort out the problem is to add a database checking method. However, I don't know how to do it in java as I use php file to connect to the server. I tried adding php codes to check if db is empty, but it doesn't work. So I'm wondering how I could check if db is empty.

This is my php file:

<?php

  //I just hide my personal info.
 $con = mysql_connect('serveraddress', 'username',  'dbpw');
mysql_select_db('dbname', $con);

$r = mysql_query('SELECT * FROM ForumData');

 while($row = mysql_fetch_array($r)){
  $out[] = $row;
}

print(json_encode($out));
mysql_close($con);

?>

This is how I retrieve the data from the db, it works when there are data in the database, but doesn't work when there is none.

private class RetrieveTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(SERVER_ADDRESS + "RetrieveReplyData.php");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), exceptionMessage + ", Ex1", Toast.LENGTH_SHORT).show();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "
");
            }
            result = sb.toString();
            System.out.println("-----JSON Data-----");
            System.out.println(result);
            is.close();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), exceptionMessage + ", Ex2", Toast.LENGTH_SHORT).show();
        }

        try {
            JSONArray jsonArray = new JSONArray(result);
            int totalCount = jsonArray.length();
            for (int i = 0; i < totalCount; i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                respondent = jsonObject.getString("reply_user");
                content = jsonObject.getString("reply_content");
                datetime = jsonObject.getString("reply_datetime");

                itemList.add(new ReplyItemList(respondent, content, datetime));
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), exceptionMessage + ", Ex3", Toast.LENGTH_SHORT).show();
        }


        return null;
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?