-2

我的个人资料模型文件:

function putProfileImage($user, $image)
{


    $record = array('user' => $user, 'profileimage' => $image);
    if ($this->exists($user))
    {
        $this->db->where('user', $user)->update('profiles', $record);
    }
    else
    {
        $this->db->where('user', $user)->insert('profiles', $record);
    }
}

function getProfileImage($user)
{

    $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'height' => 200,
            'length' => 150
    );


    $this->db->select('*')->from('profiles')->where('user', $user);
    $query = $this->db->get();
    $row = $query->row();
    return $row->profileimage; //Trying to get property of non-object error


}

它的getProfileimage,类似于我遇到最多麻烦的do_upload函数

4

3 回答 3

1

看看这些网站

然后按照下面的伪代码进行操作(假设 profileimage 在数据库中存储为 BLOB 字段)

模型文件:

function putProfileImage($user, $image)
{
    // you may need to store the image type / mine type
    $record = array('user' => $user, 'profileimage' => $image);
    if ($this->exists($user))
    {
        $this->db->update('profiles', $record)->where('user', $user);
    }
    else
    {
        $this->db->insert('profiles', $record);
    }
}

function getProfileImage($user)
{
    $this->db->select('*')->from('profiles')->where('user', $user);
    $query = $this->db->get();
    if ($query->num_rows() > 0){
        $row = $query->row();
        return $row->profileimage; //Trying to get property of non-object error
    }

    return Null;
}

控制器 - profile.php

function Upload()
{
    // see - Creating the Upload Form
    // @ - http://codeigniter.com/user_guide/libraries/file_uploading.html
}

function doUpload ()
{
    //see do_upload() and save with module above
    // @ - http://codeigniter.com/user_guide/libraries/file_uploading.html
}

function displayImage()
    {
        //Retrieve image id from the URI
        $imageid = $this->uri->segment(3);
        //Initialize the images model
        $this->load->model("image_model");
        //Call the model's getImage function passing the image id
        $image = $this->image_model->getProfileImage($imageid);
        if (!is_null($image)) {
            //need to know the mine type 
           // header('Content-Type: image/png');

            header ('Content-Type: image/jpg');
            imagejpeg(imagecreatefromstring($image), null,100);
        }
        else{
         // load a default profile image
        }
    }

    function viewProfile(){
        //load profile detail view for users and display image
        // 18 = change to image id
        echo '<img src="<?=base_url()?>profile/displayImage/18" alt="profile"/>';

    }
于 2012-10-26T04:33:56.407 回答
0

* Trying to get property of non-object *错误意味着查询很可能没有结果,即profile表中没有user=$user的行。

为了帮助您调试,您可以在$query = $this->db->get();之后使用此命令

echo $this->db->last_query(); die();

*$this->db->last_query();* 将获取最后一次查询运行的 sql,die()将终止程序。

于 2012-10-26T02:40:23.770 回答
0

我设法通过保存到正确的图像位置文件夹来解决这个问题

于 2012-11-14T01:30:49.587 回答