【问题标题】:Syntax Error in PHP APIPHP API 中的语法错误
【发布时间】:2013-03-14 02:15:05
【问题描述】:

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

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

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

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.php API,如下所示:

<?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%'";

您的建议将不胜感激。

【问题讨论】:

  • 一定要了解prepared statements,因为你的代码是SQL注入攻击的完美候选者。
  • 另外,规范链接:Why upgrade from the mysql API。请考虑使用 PDO 或 mysqli_ 函数(并使用准备好的语句)。

标签: java php android mysql json


【解决方案1】:

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

价值

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

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

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

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

【讨论】:

  • 感谢您的回复。你想多一点协作,我的意思是价值观是如何确定的?
  • 我只使用 $code $price$module 在我的应用程序中执行搜索功能,它们在我的应用程序中运行良好。
  • @OliverSmith-Jones 1. 确保按请求提供正确的参数。 2.尝试将$_POST["..."]替换为$_POST['...'];
  • 传递空变量不会出问题。
  • @DreamEater 在“LIMIT”之前没有空格。这只是因为没有那些 $_POST 变量为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2016-10-23
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多