【问题标题】:php json error fetching?php json错误获取?
【发布时间】: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 是一些东西,而不仅仅是一个空字符串。

标签: php android mysql sql oop


【解决方案1】:

在检查它的值之前,您将 $action 变量设置为空字符串两行,这就是您的代码执行 else 块的原因。 'If' 块中的内容只有在相应表达式的计算结果为 true 时才会执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多