2015-06-18 29 views
0

我指的是本教程:http://www.techfounder.net/2009/02/02/selecting-closest-values-in-mysql/选择最近的价值观在MySQL

我的查询和代码来执行搜索(这不返回任何结果)。我需要得到100个壁橱值由下式给出邮政编码这是由$postcode = $data['postcode'];

ERROR shown is: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in

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

回答

0
$sql = "SELECT Name,PostUUID,pricing,Poscode , ABS(Poscode - :distance) AS distance,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 FROM posts,subjects " 
     . "WHERE posts.subid IN (". implode(",",$key_placeholders).") " 
     . "AND posts.pricing <=:pricing " 
     . "AND posts.Poscode =:distance " 
     . "AND posts.subid=subjects.subid " 
     . "ORDER BY distance " 
     . "DESC LIMIT 100"; 
收集的用户