php代码中的SQL语法错误

时间:2016-04-01 07:45:54

标签: php mysql

我使用POST从android传递值,并使用 PHP SQL DB 中运行查询。我总是得到

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近''在第1行。

如果我直接输入值$_POST[],则代码工作正常。

我已经过了SO,但没有问题解决了我的问题。我也试过了mysql_real_escape_string($searchTermBits),但这并没有奏效。

PHP

<?php
$username    = "xxxx";
$password    = "xxxx";
$host        = "xxxx";
$database    = "xxxx";
$server      = mysql_connect($host, $username, $password);
$connection  = mysql_select_db($database, $server);
$search      = $_POST["deskey"];
$search      = explode(" ", $search);
$commonwords = "a,an,and,I,it,is,do,does,for,from,go,how,the,etc,in,on,are";
$commonwords = explode(",", $commonwords);
foreach ($search as $value)
  {
    if (!in_array($value, $commonwords))
      {
        $query[] = $value;
      }
  }

$query          = implode(" ", $query);
$searchTerms    = explode(" ", $query);
$searchTermBits = array();
foreach ($searchTerms as $term)
  {
    $term = trim($term);
    if (!empty($term))
      {
        $searchTermBits[] = "description LIKE '%$term%'";
      }
  }

$myquery = "SELECT * FROM `logins` WHERE " . implode(' OR ', mysql_real_escape_string($searchTermBits)) . "";
$query   = mysql_query($myquery);
if (!$query)
  {
    echo mysql_error();
    die;
  }
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++)
  {
    $data[] = mysql_fetch_assoc($query);
  }
echo json_encode($data);
mysql_close($server);
?>

我可以使此代码更改$_POST[]

0 个答案:

没有答案