【发布时间】:2016-06-04 09:35:13
【问题描述】:
我正在制作服务器端的安卓应用 但是我的 php 代码不能正常工作..它只显示我的 else 部分..你没有被授权......请帮助我 这是我的 php 代码 -
class DbFoodFunctions {
private $conn;
private function openDB() {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "blood";
$this -> conn = new mysqli($servername, $username, $password, $dbname);
if ($this -> conn -> connect_error) {
die("Connection failed: " . $conn -> connect_error);
}
}
private function closeDB() {
$this -> conn -> close();
}
public function searchFood($money)
{
$this -> openDB();
$stmt = $this -> conn -> prepare("Select * from FOOD where amount >= ?"); // SELECT distinct(NAME) FROM XZY WHERE Amount= ?
$stmt -> bind_param("i", $money);
$stmt -> execute();
$result = $stmt -> get_result();
$numrows = $result -> num_rows;
$this -> closeDB();
$res = array();
$res['numrows'] = $numrows;
while ($myrow = $result -> fetch_object()) {
$res[] = $myrow;
}
return $res;
}
}
?>
index.php
<?php
require_once 'db/db.php';
$dbObj = new DbFoodFunctions();
$action = '';
$result = array();
if ($action == 'searchFood') {
$money = '';
if (isset($_REQUEST['money'])) {
$money = trim($_REQUEST['money']);
}
$res = $dbObj -> searchFood($money);
$result['success'] = 1;
$result['data'] = $res;
echo json_encode($result);
}else {
$result['error'] = 1;
$result['message'] = "You are not authorized"; // i am getting this msg only
echo json_encode($result);
}
【问题讨论】:
-
$action 不是 'searchFood'
-
换句话说,您需要更改这一行:
$action = '';所以 $action 是一些东西,而不仅仅是一个空字符串。