致命错误:未捕获的PDOException:SQLSTATE [42000] :?

时间:2019-08-10 16:01:01

标签: php mysql ajax pdo

我正在尝试通过搜索构建无限滚动。到目前为止,我遇到了这个问题:

  

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DESC LIMIT 0, 4'.

有人可以帮助我解决这个问题吗?

if(isset($_GET["starts"], $_GET["limits"])){
 $search = htmlspecialchars($_GET['Search'],ENT_QUOTES,'utf-8');
$start = htmlspecialchars($_GET['starts'],ENT_QUOTES, 'utf-8');
$limit = htmlspecialchars($_GET['limits'],ENT_QUOTES, 'utf-8');
$stmt = $conn->prepare("SELECT  `jobtitle`, `company`, `location`, 
`employment`, `email`, `Description` FROM `featured job` WHERE jobtitle LIKE 
`:jobtitle` DESC LIMIT :starts, :limits");
 $stmt->bindParam(":starts", intval(trim($start)), PDO::PARAM_INT );
 $stmt->bindParam(":limits", intval(trim($limit)), PDO::PARAM_INT );
$stmt->bindParam(":jobtitle",$search);
$stmt->execute();
foreach ($posts as $data) {
  echo "<h2>".$data['jobtitle']."</h2>";
 }

这是我的ajax代码

  $(document).ready(function(){
   var limits = 4;
   var starts = 0;

   var action = 'inactive';
   function load_job_data(limits, starts)
   {
    $.ajax({
   url:"load_more.php",
   method:"GET",
   data:{limits:limits, starts:starts},
   cache:false,
   success:function(data)
   {
   $('.results').append(data);
   if(data == '')
   {
   $('#load_data_messages').text("Your potential jobs is loading");
   $('#load_data_messages').css("color", "green");

   action = 'active';
   }
   else
   {
   $('#load_data_messages').text("Out of jobs! please come back later!");
   $('#load_data_messages').css("color","red");
   action = "inactive";
   }
   }
   });
   }

   if(action == 'inactive')
   {
   action = 'active';
   load_job_data(limits, starts);
   }
   $(window).scroll(function(){
   if($(window).scrollTop() + $(window).height() > $(".load_data").height() 
&& action == 'inactive')
{
 action = 'active';
 starts = starts + limits;
 setTimeout(function(){
 load_job_data(limits, starts);
 }, 1000);
 }
 });

 });

2 个答案:

答案 0 :(得分:3)

:jobtitle不是列名,因此您不应该使用背景符号,并且会错过ORDER BY子句

SELECT  `jobtitle`
  , `company`
  , `location`
  , `employment`
  , `email`
  , `Description` 
FROM `featured job` 
WHERE jobtitle LIKE :jobtitle 
ORDER BY `jobtitle` DESC LIMIT :starts, :limits

并尝试将PARAM_STR用于$search

$stmt->bindParam(":jobtitle",$search, PDO::PARAM_STR);

答案 1 :(得分:1)

我认为您应该删除DESC关键字。如果要对其进行排序,只需在ORDER BY <column name> DESC子句前使用limit

尝试:

SELECT `jobtitle`, `company`, `location`, `employment`, `email`, `Description` FROM `featured job` WHERE jobtitle LIKE `:jobtitle` LIMIT :starts, :limits