いつも拝見させていただいてます。
今回ちょっと分からない問題にあたってしまい、質問させていただきます。
物件情報に対して、検索した住所の緯度経度から半径◯km以内にある情報の検索のため、プログラムを書いています。
php5.6
mysql5x
php
1 $lat_long = get_gps_from_address($conditions['search-text']); 2 $lat = $lat_long['lat']; 3 $long = $lat_long['lng']; 4 5 # 半径値設定 6 $radius_val = 10; 7 8 # treatment id設定 9 $treatment_id = $conditions['treatment_id']; 10 11 12$select_sql = 'select cate01.parent_category_ids as cate1, cate02.parent_category_ids as cate2, t_salon.salon_cate02, t_salon.id, t_salon.salon_name, t_salon.salon_address, t_salon.salon_tel, t_salon.salon_latitude, t_salon.salon_longitude, 13(6371 * ACOS( 14 COS(RADIANS('.$lat.')) 15 * COS(RADIANS(salon_latitude)) 16 * COS(RADIANS(`salon_longitude`) - RADIANS('.$long.')) 17 + SIN(RADIANS('.$lat.')) 18 * SIN(RADIANS(salon_latitude)) 19)) AS distance 20from t_salon 21left join t_parent_category as cate01 on t_salon.salon_cate01 = cate01.id 22left join t_parent_category as cate02 on t_salon.salon_cate02 = cate02.id 23where cate01.parent_category_face = '.$store_id.' 24having 25 distance <= '.$radius_val.' 26order by 27 distance'; 28
このような感じでsqlを作り、変数に入力された値を元に検索をかけているのですが、
たまにエラーが発生する、という状態になっています。
try, catchでも取得できなかったため、
php
1 try { 2 $data = $pdo->query($select_sql); 3 if(!$data) { 4 echo "\nPDO::errorInfo():\n"; 5 print_r($pdo->errorInfo()); 6 exit; 7 return false; 8 } 9 $array = array(); 10 while($row = $data -> fetch(PDO::FETCH_ASSOC)) { 11 $tmp = array(); 12 foreach ($row as $key => $value) { 13 $tmp[$key] = $value; 14 } 15 array_push($array, $tmp); 16 } 17 return $array; 18 } catch (PDOException $e) { 19 exit('データベースエラー : '.$e->getMessage()); 20 }
という形にしているのですが、そこで表示されたエラーが
php
1PDO::errorInfo(): Array ( [0] => 42000 [1] => 1582 [2] => Incorrect parameter count in the call to native function 'RADIANS' )
でした。
たまに発生する + 受け渡している値も正常なため、何が原因か分からなくなっている状態です。
お手数をおかけいたしますが、
ご教示お願いできますでしょうか。
よろしくお願い致します。
【追記】
変更後コード
php
1$select_sql = 'select cate01.parent_category_ids as cate1, cate02.parent_category_ids as cate2, t_salon.salon_cate02, t_salon.id, t_salon.salon_name, t_salon.salon_address, t_salon.salon_tel, t_salon.salon_latitude, t_salon.salon_longitude, 2(6371 * ACOS( 3 COS(RADIANS(:lat)) 4 * COS(RADIANS(salon_latitude)) 5 * COS(RADIANS(`salon_longitude`) - RADIANS(:lng)) 6 + SIN(RADIANS(:lat)) 7 * SIN(RADIANS(salon_latitude)) 8)) AS distance 9from t_salon 10left join t_parent_category as cate01 on t_salon.salon_cate01 = cate01.id 11left join t_parent_category as cate02 on t_salon.salon_cate02 = cate02.id 12where cate01.parent_category_face = :treatment_id 13having 14 distance <= :radius_val 15order by 16 distance'; 17 18 19 20 try { 21 $data = $pdo->prepare($select_sql); 22 # bind処理 23 $data->bindParam(':lat', $lat, PDO::PARAM_DECIMAL); 24 $data->bindParam(':lng', $long, PDO::PARAM_DECIMAL); 25 $data->bindParam(':treatment_id', $treatment_id, PDO::PARAM_STR); 26 $data->bindParam(':radius_val', $radius_val, PDO::PARAM_STR); 27 28 $data->execute(); 29 if(!$data) { 30 echo "\nPDO::errorInfo():\n"; 31 print_r($pdo->errorInfo()); 32 exit; 33 return false; 34 } 35 $array = []; 36 while($row = $data -> fetch(PDO::FETCH_ASSOC)) { 37 $array[] = $row; 38 } 39 return $array; 40 } catch (PDOException $e) { 41 exit('データベースエラー : '.$e->getMessage()); 42 }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/16 02:44