我正在尝试从服务器检查用户是否已注册,这是我的php代码:
<?php
error_reporting(E_ALL & ~E_NOTICE);
$dir = 'sqlite:/mumble-server.sqlite';
$dbh = new PDO($dir) or die("cannot open the database");
if ($_POST['func'] == 1) {
$mobileUser = $_POST['phone'];
$res_list = "SELECT count(*) FROM appusers WHERE phone ='" . $mobileUser . "'";
$rock = $dbh->query($res_list);
while ($obj = $rock->fetchObject()) {
$listing[] = $obj;
}
echo json_encode(array('reslogin' => $listing));
}
这是我的客户端(java)代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.2.26/SqliteTest/sqlite.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("func", "1"));
nameValuePairs.add(new BasicNameValuePair("phone", "09367701739"));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
UrlEncodedFormEntity form;
form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
httppost.setEntity(form);
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
MainActivity.InputStreamToStringExample str = new MainActivity.InputStreamToStringExample();
responseLogin = str.getStringFromInputStream(inputStream);
Log.e("response", "response -----" + responseLogin);
jsonresponse = new JSONObject(responseLogin);
responsemain = jsonresponse.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
但是我从服务器收到此响应:
response -----<br /><b>Fatal error</b>: Uncaught Error: Call to a member function fetchObject() on boolean in W:\xampp\htdocs\SqliteTest\sqlite.php:16Stack trace:#0 {main} thrown in <b>W:\xampp\htdocs\SqliteTest\sqlite.php</b> on line <b>16</b><br />
,第16行为while ($obj = $rock->fetchObject()) {
有谁知道我该如何解决?
我将PHP更改为
<?php
error_reporting(E_ALL & ~E_NOTICE);
$dir = 'sqlite:/mumble-server.sqlite';
$dbh = new PDO($dir) or die("cannot open the database");
if ($_POST['func'] == 1) {
$mobileUser = $_POST['phone'];
$rock = $dbh->prepare("SELECT count(*) FROM appusers WHERE phone ='" . $mobileUser . "'");
$result = $rock->fetchAll(\PDO::FETCH_ASSOC);
echo json_encode(array('reslogin' => $result));
}
但是现在出现此错误:
response -----<br /><b>Fatal error</b>: Uncaught Error: Call to a member function fetchAll() on boolean in W:\xampp\htdocs\SqliteTest\sqlite.php:8Stack trace:#0 {main} thrown in <b>W:\xampp\htdocs\SqliteTest\sqlite.php</b> on line <b>8</b><br />