var_dump一个变量在php

时间:2016-06-17 02:24:40

标签: php json codeigniter

我有一些变量并尝试回显它们但我看到空结果(在实时服务器上测试)。我找不到我的错误,请帮忙..

我的控制器:

function request($req)
{
    $tiki_url = "http://203.77.231.130/services/api.cfc?method=tariff&origin=CGK01.00&destination={TO_AREA}&weight=1";
    var_dump($tiki_url);

    $code = array();
    $this->db->select('*');
    $this->db->where('request_id = ', $req );
    $query = $this->db->get('excel');
    $sql= $query->result_array(); 
    var_dump($sql);

    foreach ($sql as $key) 
    {
        $ex =$key['excel_id'];
        $zipcode = $key['to_zipcode'];
        $request = $key['request_id'];
        $this->db->select('tariff_code');
        $this->db->where('zip_code = ', $zipcode );
        $query = $this->db->get('area_info'); 
        $tarif= $query->row_array();
        $kodepos = $tarif['tariff_code'] ;
        var_dump($kodepos);

        if(isset($code[$kodepos]))
        {
            $tiki_data = $code[$kodepos];
        } 
        else
        {
            $url = str_replace("{TO_AREA}",$kodepos,$tiki_url);
            $tiki_data = json_decode(file_get_contents($url),TRUE);
            $code[$kodepos] = $tiki_data ;
        }

        $tariff = $tiki_data['tariff'][3]['TARIFF'] ;
        var_dump($tariff);

        $data = array(
                'tariff'=>$tariff,
                );
        var_dump($data);

        $data_status = array(
            'status' => '2',
            );
        var_dump($data_status);

        $this->db->update('excel', $data, "excel_id =".$ex);
        $this->db->update('request',$data_status, "request_id=".$request);

        var_dump($key);

        echo json_encode(array("status" => TRUE, "total" => $tariff,"excel" => $ex ));
    }
}

除了var_dump($tiki_url)之外,所有var_dump()都不返回任何内容。我确信excel表中有几条记录。我期望这个结果来自回显json_encode(我从localhost上的测试中捕获它):

enter image description here

1 个答案:

答案 0 :(得分:2)

只有第一个返回数据,因此我们可以猜测SQL查询存在错误。您可以使用is_array()功能进行检查。

...
$sql = $query->result_array();
if ( ! is_array($sql)) {
    exit('Error.');
}
var_dump($sql);
...