php - 使用 CodeIgniter 从数据库返回字符串

标签 php mysql codeigniter

这是我的模型:

function getAvenuName($id) {
        $this->db->distinct();
        $this->db->select('admin.username');
        $this->db->from('personal_closest');
        $this->db->join('admin', 'admin.id = personal_closest.avenu_id', 'left');
        $this->db->where('personal_closest.avenu_id', $id);
        $query = $this->db->get();
        return $query->result();        
    }

这是我的 Controller :

foreach ($listofcloset['postByUser'] as $key => $value) { 
            $listofcloset['postByUser'][$key]['calculatetime']   =  $this->calculatetime(strtotime($value['postdatetime']));
            $listofcloset['postByUser'][$key]['countcomments']   =  $this->countcomments($value['id']); 
            $listofcloset['postByUser'][$key]['comments']        =  $this->comments->sltpostcomments($value['personal_closest_id']); 
            $listofcloset['postByUser'][$key]['countCloset']     =  $this->closet->countCloset($value['id']);            
            $listofcloset['postByUser'][$key]['givencool']       =  $this->checkCoolIsGiven($this->session->userdata('user_id'),$value['user_id'],$value['personal_closest_id']);
            $listofcloset['postByUser'][$key]['givencloset']     =  count($this->closet->selectIdbycloset($this->session->userdata('user_id'),$value['user_id'],$value['personal_closest_id']));
            if (empty($listofcloset['postByUser'][$key]['username'])){
                $listofcloset['postByUser'][$key]['username'] = $this->personal_closest->getAvenuName($value['avenu_id']);
            }
        }

当我获取用户名时,我遇到了数组到字符串转换的问题。我正在尝试找到一种解决方案,仅将字符串替换为数组。

这就是结果:

[username] => Array
            (
                [0] => stdClass Object
                    (
                        [username] => USER1
                    )

            )

最佳答案

这应该能得到它:)

function getAvenuName($id) {
    $this->db->distinct();
    $this->db->select('admin.username');
    $this->db->from('personal_closest');
    $this->db->join('admin', 'admin.id = personal_closest.avenu_id', 'left');
    $this->db->where('personal_closest.avenu_id', $id);
    $query = $this->db->get();
    $result = $query->row();
    return $result->username;  
}

关于php - 使用 CodeIgniter 从数据库返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29898784/

相关文章:

php - MySQL 显示逗号​​分隔字符串的值

php : file path for windows azure code

php - 尝试上传图像时出现 undefined index 错误

php - 此场景中正确的 mysql 表设计/关系

mysql - phpmyadmin 运行速度非常慢(information_schema)

mysql - 如何根据表中选择的百分比将行值分配给不同的值

javascript - 比较日期 - 结束日期应大于开始日期

php - 对表中 4 列的数组进行排序,以便按每列的字母顺序读取

php - 如何使用 jQuery AJAX 和 PHP 数组返回

项目的php索引