-1

Parse error: syntax error, unexpected ';', expecting ',' or ')' in /home/u459249666/public_html/ss/search.php on line 19

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

On Running this script I'm getting

parse error in line 19

but I am not able to understand what the error is how can it be corrected.

I tried a lot. Since I m beginner it is very confusing sometimes. Please help me out

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
tnavin56
  • 21
  • 1

2 Answers2

0

You have an error in php. Don't parse it in html entity name

it should be as follow.

   <?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;

 }

?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

You can get values in $_GET array like this. Follow the following method if you want to get s variable's value from $_GET array

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