Я пытаюсь проверить с сервера, зарегистрирован ли пользователь или нет, вот мой 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()) {
Кто-нибудь знает, как я могу это исправить?
Iизменил мой 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 />