Skip to main content

error akses database

June 6, 2011 by hery_yud

hery_yud's picture

aslkm
gan ane newbie nih lg mau cb akses data dr database tp kok error ya.
Tampilan errornya gini gan

A PHP Error was encountered

Severity: Notice

Message: Undefined property: M_helloworld::$db

Filename: models/m_helloworld.php

Line Number: 16

Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\HelloWorld\application\models\m_helloworld.php on line 16

c_helloworld.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class C_helloworld extends CI_Controller {
 
	function __construct()
	{
		parent::__construct();
 
	}
 
 
	function index()
	{
		$this->load->model('M_helloworld');
		$data['teks'] = $this->M_helloworld->mHello();
		$data['siswa'] = $this->M_helloworld->all();
		$this->load->view('v_helloworld',$data);
	}
}

M_helloworld.php

<?php
class M_helloworld extends CI_Controller {
 
	function __construct()
	{
		parent::__construct();
	}
 
	function mHello(){
		$teks = "Hello World";
		return $teks;
	}
 
	function all()
	{
		$this->db->get('maha');
	}
}
 
?>

V_helloworld.php

<html>
<head>
<Title>Testing Hello World</Title>
</head>
 
<body>
<?php	
	echo $teks;	
	echo '<br>';
?>
<?php foreach($siswa as rows) ?>
$rows->nim
<?php endforeach ?>
</body>
</html>

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

<?php class M_helloworld

June 7, 2011 by cahndeso, 8 weeks 2 days ago
Comment: 8315

cahndeso's picture

<?php
class M_helloworld extends CI_Controller {
....
}

ganti
<?php
class M_helloworld extends CI_Model {
....
}

jangan lupa load library database (masukkan di autoload).

untuk tampilkan data di view
ubah :

<?php foreach($siswa as rows) ?>
$rows->nim
<?php endforeach ?>

menjadi
<?php 
foreach($siswa->result() as $rows):
    $rows->nim;
endforeach;
?>

(baca kembali user_guide/database/active_record.html#select)..

semoga membantu

Happy Coding

Cahndeso

Premium Drupal Themes by Adaptivethemes