三个类似的MySql查询,一个不起作用

时间:2013-08-21 20:06:38

标签: mysql wordpress

我不是一个php程序员,但是将三个非常相似的查询拼接到同一个表中,前两个工作,最后一个函数get_adres没有返回任何输出。有什么不对请? 编辑:前两个函数工作正常,最后一个get_adres()返回逗号。

function get_posit() {
    global $wpdb;
    $tbl_ads = $wpdb->prefix."awpcp_ads";
    $adid = get_query_var('id');
    $ad_lat = $wpdb->get_var($wpdb->prepare("SELECT geo_lat FROM $tbl_ads WHERE ad_id='$adid';"));
    $ad_lon = $wpdb->get_var($wpdb->prepare("SELECT geo_lon FROM $tbl_ads WHERE ad_id='$adid';"));
    $adpos = $ad_lat.",".$ad_lon;
    return $adpos;
}

function get_titles() {
    global $wpdb;
    $tbl_ads = $wpdb->prefix."awpcp_ads";
    $adid = get_query_var('id');
    $adtitle = 'Position '.$wpdb->get_var($wpdb->prepare("SELECT ad_title FROM $tbl_ads WHERE ad_id='$adid';"));
    return $adtitle;
}

function get_adres() {
    global $wpdb;
    $tbl_ads = $wpdb->prefix."awpcp_ads";
    $adid = get_query_var('id');
    $street = $wpdb->get_var($wpdb->prepare("SELECT adcontact_countyvillage FROM $tbl_ads WHERE   ad_id='$adid';"));
    $plz = $wpdb->get_var($wpdb->prepare("SELECT adcontact_state FROM $tbl_ads WHERE ad_id='$adid';"));
    $city = $wpdb->get_var($wpdb->prepare("SELECT adcontact_city FROM $tbl_ads WHERE ad_id='$adid';"));
    $adrs = $street.",".$plz.",".$city;
    return $adrs;
}

1 个答案:

答案 0 :(得分:0)

get_var返回单个值。对于底部的查询,请尝试向其添加限制1,所以

    $street = $wpdb->get_var($wpdb->prepare("SELECT adcontact_countyvillage FROM $tbl_ads WHERE   ad_id='$adid' LIMIT 1;"));
    $plz = $wpdb->get_var($wpdb->prepare("SELECT adcontact_state FROM $tbl_ads WHERE ad_id='$adid' LIMIT 1;"));
    $city = $wpdb->get_var($wpdb->prepare("SELECT adcontact_city FROM $tbl_ads WHERE ad_id='$adid' LIMIT 1;"));