-1

I am trying to get a search working, I have echoed and used var dump and what I am getting in my browser is:

0 there was no search results

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) 
                              ["field_count"]=> int(4) 
                              ["lengths"]=> NULL 
                              ["num_rows"]=> int(0) 
                              ["type"]=> int(0) 
                            }

But I have typed in Paris which is in my database, so it shouldn't echo there was no search.

Below is my code. Not sure where I am going wrong, any help would be appreciative.

require_once('config1.php');
error_reporting(E_ALL);

$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];

$sql = "SELECT a.attraction_name, a.lat, a.long, a.cost FROM zz_attractions a 
INNER JOIN zz_city c ON a.city_id = c.city_id WHERE c.city_name LIKE 
'%searchq%'";

$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);

if ($count == 0) { 
    $output = 'there was no search results';

    echo $count;
    echo $output;
    var_dump($result);
} else {
    while ($row = mysql_fetch_array($sql)) { 
    $attraction_name = $row['attraction_name'];
    $lat = $row['lat'];
    $long = $row['long'];
    $cost = $row['cost'];

    $output .= '<div>'.$attraction_name.' '.$lat.' '.$long.' '.$cost.'</div>';

    }
}
}


city_id  attraction_id  attraction_name  lat  long  cost
1           1             Eiffel Tower   49    2     25
Kl12
  • 17
  • 5

1 Answers1

2

Your're missing the $ off the variable that you're passing into your SQL statement. Try

$sql = "SELECT a.attraction_name, a.lat, a.long, a.cost FROM zz_attractions a 
INNER JOIN zz_city c ON a.city_id = c.city_id WHERE c.city_name LIKE 
'%$searchq%'";

Also, have a look at binding parameters to sql statements rather than passing them in directly as your current code leaves you vulnerable to injection https://www.w3schools.com/sql/sql_injection.asp | http://php.net/manual/en/mysqli-stmt.bind-param.php

gnusey
  • 354
  • 3
  • 16