2017-02-25 90 views
-1

Parse error: syntax error, unexpected ';', expecting ',' or ')' in /home/u459249666/public_html/ss/search.php on line 19PHP的錯誤:解析錯誤

$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; 
} 

上運行此腳本我越來越

parse error in line 19

,但我無法理解的錯誤是什麼它如何能得到糾正。

我嘗試了很多。因爲我是初學者,有時候這很混亂。請幫我出來

回答

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; 

} 

?> 
0

您可以像這樣在$ _GET數組中獲取值。如果要從$ _GET數組中獲取s變量的值,請遵循以下方法

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