我需要靠近zipcode列表数据zipcode,所以我使用的示例表名是li\\U zipcode:
邮政编码纬度经度35005.604744-87.042442-3我的代码:
$zipcode = 35004;
$zipdetail = $wpdb->get_results( "SELECT * FROM `li_zipcode` WHERE `zip_code` = \'$zipcode\'", OBJECT );
$latitude = $zipdetail->latitude;
$longitude = $zipdetail->longitude;
$radius = 40; //## this is 40 miles from given zipcode
//## Now get list of zipcode nearby given zipcode
$ziplist = $wpdb->get_results( "SELECT zip_code, ( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) AS distance FROM li_zipcode HAVING distance < 25", OBJECT );
echo \'<pre>\';
print_r($ziplist);
但其显示错误如下:
WordPress database error: [Incorrect parameter count in the call to native function \'radians\']
SELECT zip_code, ( 3959 * acos( cos( radians() ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians() ) + sin( radians() ) * sin( radians( latitude ) ) ) ) AS distance FROM li_zipcode HAVING distance < 25
如果我在phpmyadmin中运行,它工作正常:
MySQL查询:SELECT zip_code, ( 3959 * acos( cos( radians(33.604744) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-87.042442) ) + sin( radians(33.604744) ) * sin( radians( latitude ) ) ) ) AS distance FROM li_zipcode HAVING distance < 25
显示结果:
请帮助我。
最合适的回答,由SO网友:kero 整理而成
很可能是你的价值观$latitude
和/或$longitude
不是你所期望的那样。所以radians($latitdue)
成为radians()
或radians(\'\')
.
要获得一行,get_row()
通常是更好的选择,而不是get_results()
.
$zipdetail = $wpdb->get_row( "SELECT * FROM `li_zipcode` WHERE `zip_code` = \'$zipcode\'", OBJECT );
如果
$zipcode
来自不受信任的来源(例如用户输入),请确保
use a prepared statement:
$zipdetail = $wpdb->get_row(
$wpdb->prepare(
\'SELECT latitude, longitude FROM `li_zipcode` WHERE `zip_code` = %d\',
$zipcode
),
OBJECT
);