PHP错误:解析错误

时间:2017-02-25 09:51:59

标签: php

  在第19行的/home/u459249666/public_html/ss/search.php中

解析错误:语法错误,意外';',期待','或')'

$confiq = array(
    'host' => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname' => 'hunklessons',
);

$db = new PDO('mysql:host='.$confiq['host'].';dbname='.$confiq['dbname'],$confiq['username'],$confiq['password'].'');

if(isset($_GET['s']) && !empty($_GET['s'])) {

    //secure the search input
    $search = trim(strip_tags($_GET['s']));

    //convert the space in the search to sepreate terms
    $search_terms = explode(" ", $search);

    $term_count = 0;
    $q = "";
    $result = array();
    $i = 0;

    foreach ($search_terms as $term) {
        $term_count++;
        if($term_count === 1) {
            $q .= "`title` LIKE '%$term%' "; 
        } else {
            $q .= "AND `title` LIKE '%$term%' ";
        }
    }

    //prepare the mysql query in PDO
    $query = $db->query("SELECT * FROM `google_search` WHERE $q");

    //get the number of the results found
    $num = $query->rowCount();

    if ($num > 0) {
        //fetch the result
        while($row = $query->fetch(PDO::FETCH_ASSOC)){
            //put the results in the array
            $result[$i] =  array(
                'title' => $row['title'],
                'desc' => $row['description'],
                'link' => $row['link']
            );
            $i++;
        }
    }

    //convert result array into json format
    $json_result = json_encode($result);

    echo $json_result;
}

运行此脚本我正在

  

第19行解析错误

但我无法理解错误是如何纠正的。

我尝试了很多。由于我是初学者,有时候会很困惑。请帮帮我

2 个答案:

答案 0 :(得分:0)

你在php中有错误。不要在html实体名称中解析它

应该如下。

   <?php

 //connect to the db

 $confiq = array(

  'host' => 'localhost',

  'username' => 'root',

  'password' => '',

  'dbname' => 'hunklessons',

 );



 $db = new PDO('mysql:host='.$confiq['host'].';dbname='.$confiq['dbname'],$confiq['username'],$confiq['password'].'');

?>



<?php

 if(isset($_GET['s']) && !empty($_GET['s'])){ //<----------- change this line

  //secure the search input

  $search = trim(strip_tags($_GET['s'])); //<----------- change this line



  //convert the space in the search to sepreate terms

  $search_terms = explode(" ", $search);

  $term_count = 0;

  $q = "";

  $result = array();

  $i = 0;



  foreach ($search_terms as $term) {

   $term_count++;

   if($term_count === 1){

    $q .= "`title` LIKE '%$term%' "; 

   }else{

    $q .= "AND `title` LIKE '%$term%' ";

   }

  }



  //prepare the mysql query in PDO

  $query = $db->query("SELECT * FROM `google_search` WHERE $q");



  //get the number of the results found

  $num = $query->rowCount();



  if($num > 0){

   //fetch the result

   while($row = $query->fetch(PDO::FETCH_ASSOC)){

    //put the results in the array

     $result[$i] =  array(

      'title' => $row['title'],

      'desc' => $row['description'],

      'link' => $row['link']

     );

     $i++;

   }

  }



  //convert result array into json format

  $json_result = json_encode($result);

  echo $json_result;

 }

?>

答案 1 :(得分:0)

您可以像这样在$ _GET数组中获取值。如果要从$ _GET数组

获取变量值,请遵循以下方法
 if(isset($_GET['s']) && !empty($_GET['s'])){