I'm referring to this tutorial: http://www.techfounder.net/2009/02/02/selecting-closest-values-in-mysql/

My query and code to execute search (This doesn't return any result).I need to get 100 closets value to the postcode given by user which is collected by $postcode = $data['postcode'];
ERROR shown are:

 1)  Fatal error: in C:\wamp\www\search\ajax2.php on line 79
 2)  PDOException: in C:\wamp\www\search\ajax2.php on line 79

My Query

$return = $_POST;
 //$return='{"sub":{"1":"","2":""},"postcode":"56000","slider1":"100","action":"test"}';

 $return["json"] = json_encode($return);
 // echo json_encode($return);
 $data = json_decode($return["json"], true);
  //var_dump($data);

include($_SERVER['DOCUMENT_ROOT']."/config.php");


$key =array_keys($data['sub']); 
//print_r($key);
$pricing2 = $data['slider1'];
$pricing = $pricing2 * 1.15;
$postcode = $data['postcode'];



$bindings = array();
$bindings[] = array(":pricing", $pricing, PDO::PARAM_STR);
$bindings[] = array(":distance", $postcode, PDO::PARAM_STR);

$key_placeholders = array();
foreach($key as $k => $v) {
    $placeholder = ":subid".$k;
    $bindings[] = array($placeholder, $v, PDO::PARAM_INT);
    $key_placeholders[] = $placeholder;
}


$sql = "SELECT Name,PostUUID,pricing,Poscode,subname,Reputation,ReviewPlus,ReviewNeg,week_morning,week_afternoon,week_evening,weekend_morning,week_afternoon,week_evening,weekend_morning,weekend_afternoon,week_evening,weekend_morning,weekend_afternoon,weekend_evening,date,Phone,Sex,UUID,catname FROM posts,subjects "
        . "WHERE posts.subid IN (". implode(",",$key_placeholders).") "
        . "AND posts.pricing <=:pricing "
        . "AND posts.Poscode, ABS( posts.Poscode - :distance ) AS distance " distance "
        . "AND posts.subid=subjects.subid "
        . "ORDER BY distance "
        . "DESC LIMIT 100";

$statement = $pdo->prepare($sql);
foreach($bindings as $b) {
    $statement->bindValue($b[0],$b[1],$b[2]);
}
$statement->execute();
$json = array();
while( $row = $statement->fetch()) {
    array_push($json, array("name" => $row['Name'], "id" => $row['PostUUID'],"rate" => $row['pricing'],"postcode" => $row['distance'],"subject" => $row['subname'],"reputation" => $row['Reputation'],"plus" => $row['ReviewPlus'],"neg" => $row['ReviewNeg'],"weekM" => $row['week_morning'],"weekA" => $row['week_afternoon'],"weekE" => $row['week_evening'],"endM" => $row['weekend_morning'],"endA" => $row['weekend_afternoon'],"endE" => $row['weekend_evening'],"postDate" => $row['date'],"contact" => $row['Phone'],"gender" => $row['Sex'],"tid" => $row['UUID'],"levelName" => $row['catname']));
}
header('Content-Type: application/json');
echo json_encode($json);

If you enable PDO exception handling, then you might be able to find out more about the error. Read here how to do that.

Also check line 35.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.