从mysqli获取数据时未定义的变量

时间:2015-07-24 05:16:27

标签: php mysqli

我是php的新手我想尝试将手机转换为时区转换器。 我收到错误未定义的变量' $ result'在第49行 我该如何解决它。 如果你能找到我做过的任何其他错误的话 请查看我正在制作的错误                                                                        电话号码:

$tier0_languages = array("en_US");
$tier1_languages = array("ja_JP", "de_DE", "fr_FR");
$tier2_languages = array("it_IT","es_ES","es_MX","nl_NL","pt_BR","da_DK","fi_FI","nb_NO","sv_SE");
$tier3_languages = array("ko_KR","zh_CN","zh_TW");
$tier4_languages = array("cs_CZ","ru_RU","tr_TR","pl_PL","hu_HU","uk_UA","fr_MA","en_AE","en_IL");

$tier = array_merge($tier0_languages,$tier1_languages,$tier2_languages,$tier3_languages,$tier4_languages);

uasort($arr, function ($a,$b) use ($tier){
    foreach($tier as $value){
        if($value == $a['primary_language']){
            return 0;
            break;
        }
        if($value == $b['primary_language']){
            return 1;
            break;
        }
    }
});

print_r($arr);

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我相信这样会有效。我不确定$ code,如果他包含正确的信息,你可以通过echo $ code来检查。

    if(preg_match($pattern, $ccode, $matches))
    {
        $code = $matches[0];
    } else { $code = substr($ccode, 0, 4); }

    $q = "SELECT nicename FROM country WHERE phonecode=$code";
    if($query = $db->query($q)) 
    {
        $record = $query->fetch_assoc();
        echo '<pre>';
        print_r($record);
    } else { echo "<br/>NO RECORD"; }


    $abb = "SELECT zone_id FROM zone INNER JOIN country on country.country_code = zone.country_code
            WHERE country.country_name = '" . $record['nicename'] . "'";
    if($query = $db->query($abb)) 
    {
        $result = $query->fetch_assoc();
        echo '<pre>';
        print_r($result);
        //Here $result will exist!
    } else { echo "<br/>NO RECORD"; }
    // You had your $result here, and here, the result may not exist, 
    // depending if the query succeeded or not.
    // Same counts for the query above.

据我所知,有两个问题。

1是你试图打印$ record,而$ record可能不存在,并且$ result的结果相同。

2是区域查询,如果检查列/字段是否等于某个字符串,则确保该字符串在其周围有引号。所以country_name ='something'。你在$ record ['nicename']附近没有这些引号,所以无论是$ record ['nicename'],他都认为这是一个列/字段,但不是值得检查的。