1

我正在开发一个 android 应用程序,我需要一个 PHP API 才能让我的应用程序与 SQL 数据库通信。

我在解析 JSON 时出错getJarrayFromString();

然后我尝试记录实际错误,如下所示:

03-24 15:41:12.175: E/JustDealsUtils(480): Error parsing to json on getJarrayFromString(); org.json.JSONException: Value Database of type java.lang.String cannot be converted to JSONArray
03-24 15:41:12.175: E/log_tag(480): Failed data was:
03-24 15:41:12.175: E/log_tag(480): Database query failed You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bCode` LIKE '%%' AND `bTitle` LIKE '%%' AND `bModule` LIKE '%%'' at line 1

现在上面的日志很好地阐明了错误在于我的books.phpAPI,如下所示:

<?php
    include("MysqlConnection.php");
    header('Content-Type: application/json');
    $from = $_POST["from"];
    $nr = $_POST["nr"];
    // those variables are for search
    $title = $_POST["title"];
    $code = $_POST["code"];
    $price = $_POST["price"];
    $module = $_POST["module"];
    $order = $_POST["order"];
    $by = $_POST["by"];


    $sql = "SET CHARACTER SET utf8";
    $db->query($sql);

    if(isset($from) && isset($nr)){
        // we need to know how many rows are in total for this query
<PROBLEM IS HERE---> $sql = "SELECT * FROM `books` WHERE `bSpecialOffer`='false' AND `bCode` LIKE '%$code%' AND `bTitle` LIKE '%$title%' AND `bModule` LIKE '%$module%'";
        $query = $db->query($sql);

        $rows = array();
        $rows[] = array("numRows"=>$db->numRows($query));

        // if those 2 var are set then we order the query after them
        if(isset($order) && isset($by)){
            $sql .= " ORDER BY `$order` $by LIMIT $from, $nr";
        }else{
            $sql .= "LIMIT $from, $nr";
        }

        $query = $db->query($sql);

        if($db->numRows($query)!=0){
            while($row = mysql_fetch_assoc($query)) {
                $rows[] =  $row;
            }
            echo json_encode($rows);
        }
    }

    $db->closeConnection();
?>

我很困惑我的陈述在 PHP 中是如何错误的,正如上面在 Log Cat 中提到的那样!

有没有另一种写法:

$sql = "SELECT * FROM `books` WHERE `bSpecialOffer`='false' AND `bCode` LIKE '%$code%' AND `bTitle` LIKE '%$title%' AND `bModule` LIKE '%$module%'";

您的建议将不胜感激。

4

1 回答 1

0

sql 查询到目前为止是正确的,但是您传递的值是空的。

的价值

    $code = $_POST["code"];
    $price = $_POST["price"];
    $module = $_POST["module"];

是空的。尝试确保已设置该值。

有时双引号对此负责。然后尝试用单引号替换它们

    $code = $_POST['code'];
    $price = $_POST['price'];
    $module = $_POST['module'];
于 2013-03-24T15:54:44.320 回答