我无法在 codeigniter 中显示我的数据库

I can't show my database in codeigniter

我想我的程序没有问题。我总是收到这样的错误消息:

"Fatal error: Call to a member function result() on null in C:\xampp\htdocs\tenda_online\application\views\view_photo.php on line 34"

这是我的程序:

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Photo extends CI_Controller {

public function __construct()
{
    parent::__construct();

    $this->load->model('model_photo');
    $this->load->library('pagination');
}

public function index()
{
    //------------------------------------------------------------------------------------
    $getData = $this->db->get('photo');
    $a = $getData->num_rows();
    $config['base_url'] = site_url().'/photo/index'; //set the base url for pagination
    $config['total_rows'] = $a; //total rows
    $config['per_page'] = '6'; //the number of per page for pagination
    $config['uri_segment'] = 3; //see from base_url. 3 for this case
    $config['full_tag_open'] = '<p class=pagination>';
    $config['full_tag_close'] = '</p>';
    $this->pagination->initialize($config); //initialize pagination
    //------------------------------------------------------------------------------------  

    $data['judul'] = 'Photo Album';

    $data['photo'] = $this->model_photo->get_photo($config['per_page'],$this->uri->segment(3));

    $this->load->view('view_photo', $data);
}

public function tambah()
{
    $data['error'] = '';

    $data['judul'] = 'Photo Album';

    $this->load->view('tambah', $data);
}

public function proses_tambah()
{

    $this->load->library('form_validation');

    $nama_asli = $_FILES['userfile']['name'];

    $judul = $this->input->post('judul',TRUE);

    // Konfigurasi Upload Gambar

    $config['file_name'] = $judul.'_'.'_'.$nama_asli;
    $config['upload_path'] = './application/views/photo';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1024';
    $config['max_width']  = '1600';
    $config['max_height']  = '1200';

    // End Konfigurasi Upload Gambar

    // Memuat Library Upload File
    $this->load->library('upload', $config);

    $this->form_validation->set_message('required', '%s is required.');
    $this->form_validation->set_message('min_length', '%s Minimal %s Karakter.');
    $this->form_validation->set_message('max_length', '%s Maksimal %s Karakter.');
    $this->form_validation->set_message('is_unique', '%s Telah Terdaftar');
    $this->form_validation->set_message('matches', '%s Tidak Cocok dengan %s.');
    $this->form_validation->set_message('numeric', '%s Harus diisi Angka.');

    $this->form_validation->set_rules('judul', 'Title', 'trim|required|is_unique[photo.title]');

    if (($this->form_validation->run() === FALSE) || (! $this->upload->do_upload()))
    {
        // Jika Konfigurasi tidak cocok :
        $data = array('error' => $this->upload->display_errors('<span class="error">','</span>'));  

        $data['judul'] = 'Photo Album';

        $this->load->view('tambah', $data);

    }
    else{

        $data = array('upload_data' => $this->upload->data());

        $tanggal = date('Y-m-d');

        $get_name = $this->upload->data();
        $nama_foto = $get_name['file_name'];

        $this->model_photo->tambah_photo($nama_foto,$tanggal);

        redirect('photo','refresh');

    }

}

public function proses_hapus($id_photo)
{

    $photo = $this->model_photo->link_photo($id_photo);

    if ($photo->num_rows() > 0)
    {

        $row = $photo->row();

        $file_photo = $row->photo_url;

        $path_file = './application/views/photo/';
        unlink($path_file.$file_photo);

    }   

    $this->model_photo->hapus_photo($id_photo);

    redirect('photo','refresh');

}


}
?>

型号:

<?php

Class Model_photo extends CI_Model {

public function __construct()
{

    $this->load->database();

}

function tambah_photo($nama_foto,$tanggal) {

    $data = array(
        'title' => $this->input->post('judul'),
        'photo_url' => $nama_foto,
        'created' => $tanggal
    );

    return $this->db->insert('photo', $data);

}

function get_photo($perPage, $uri) {


    $this->db->order_by('id_photo','DESC');

    $query = $getData = $this->db->get('photo', $perPage, $uri);

    if($getData->num_rows() > 0)

    return $query;

    else

    return null;

}

function link_photo($id_photo){

    $this->db->where('id_photo',$id_photo);
    $query = $getData = $this->db->get('photo');

    if($getData->num_rows() > 0)
    return $query;
    else
    return null;

}

function hapus_photo($id_photo) {

    $this->db->where('id_photo',$id_photo);
    $this->db->delete('photo');

}

}

?>

查看:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo"$judul"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index,follow" />

<!-- CSS -->
<link href="application/views/themes/css/styles.css" rel="stylesheet" type="text/css"  />
</head>

<body>

<div class="konteiner">

<header>
<h1><?php echo"$judul";?></h1>
</header>



<div class="home">

<?php echo anchor('photo/tambah',"Add Photo");?>

<div class="clear">

</div>

<?php

foreach($photo->result() as $row):?>

<div class="image">

    <?php       

            $image = array(
              'src' => 'application/views/photo/'.($row->photo_url),
              'alt' => ($row->title),
              'class' => 'photo',
              'width' => '240',
              'height' => '180',
              'title' => ($row->title),
              'rel' => 'lightbox',
            );

            echo img($image); ?>

    <div class="detail">
        <?php echo($row->title);?>
        <br/>
        Publish : <?php echo($row->created);?> |

    <?php echo anchor('photo/proses_hapus/'.$row->id_photo,"Delete");?>
    </div>

</div>


<?php endforeach; ?>

<div class="clear">

</div>

<?php echo $this->pagination->create_links(); ?>

</div>



<footer>
<p>Demo - Codeigniter | 2013</p>
</footer>

</div>

</body>
</html>

我的数据库:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'tendaonline';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

get_photo returns null 在没有照片的情况下,因此当 $photo 为 null 而不是对象时,您无法访问 $photo->result()

在您的模型中,当没有行时,不要 return null,只 return 结果集。无需检查模型中的行数。当没有行时,$query->result() 将是一个空数组,并且 foreach 在这种情况下不会自动 运行。