Welcome Guest, Not a member yet? Register   Sign In
database access by code igniter
#1

[eluser]subhashramesh[/eluser]
Hi all,could you please respond ............
I am new to codeigniter and i tried to access databse as follows.
my databse name is ‘efo’ and and it has table ‘sample’ with 3 columns namely a,b,c respectively.

CONFIG.PHP settings i changed as follows.
$config[‘base_url’] = “https://127.0.0.1/CodeIgniter_1.7.1/”;
$config[‘rewrite_short_tags’] = TRUE;

DATABSE.PHP settings i changed as follows.
$db[‘default’][‘hostname’] = “localhost”;
$db[‘default’][‘username’] = “root”;
$db[‘default’][‘password’] = “”;
$db[‘default’][‘database’] = “efo”;
$db[‘default’][‘dbdriver’] = “mysql”;
$db[‘default’][‘dbprefix’] = “”;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = “”;
$db[‘default’][‘char_set’] = “utf8”;
$db[‘default’][‘dbcollat’] = “utf8_general_ci”;


AUTOLOAD.PHP i changed settings as follows.
$autoload[‘libraries’] = array(‘database’);

my model file as Helloworld_model.php as follows.
<?php
class Helloworld_model extends Model {

function Helloworld_model()
{
// Call the Model constructor
parent::Model();
}

function getData()
{
//Query the data table for every record and row

$query = $this->db->get(‘sample’);

if ($query->num_rows() < 0)
{
//show_error(‘Database is empty!’);
}else{
return $query->result();
}
}

}
?&gt;

my controlfile Helloworld.php as follows.
&lt;?php
class Helloworld extends Controller{
function index()
{
$this->load->model(‘Helloworld_model’);

$data[‘result’] = $this->Helloworld_model->getData();
$data[‘page_title’] = “CI Hello World App!”;

$this->load->view(‘helloworld_view’,$data);
}
}
?&gt;

my view file helloworld_view.php as follows.
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$page_title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php foreach($result as $row):?&gt;


&lt;?=$row->a?&gt;

&lt;?=$row->b?&gt;
&lt;?=$row->c?&gt;


&lt;?php endforeach;?&gt;
&lt;/body&gt;
&lt;/html&gt;
but i am getting error as follows.

A PHP Error was encountered
Severity: Notice

Message: Undefined property: Helloworld::$Ok1

Filename: libraries/Loader.php

Line Number: 1038
Fatal error: Call to a member function _assign_libraries() on a non-object in C:\xampp\htdocs\CodeIgniter_1.7.1\system\libraries\Loader.php on line 1038
could you please anyone solve this problem.

Profile PM
Edit Quote
#2

[eluser]jedd[/eluser]
Hello subhashramesh and welcome to the CI forums.

It looks like you've forgotten your constructor in your controller.

You might also want to have a quick play with the [ code ] tags - they're very handy for making this stuff look much prettier!
#3

[eluser]Aishwarya.M.B[/eluser]
rename the codeigniter folder as ci,
then u try this in config.php..
hope this will help
if u still have probs, u can send me a private mail to [email protected]

$config['base_url'] = "http://localhost/ci/"
#4

[eluser]Aishwarya.M.B[/eluser]
you can also refer to the following link...
there'll be clear instructions given step by step
http://net.tutsplus.com/tutorials/php/co...er-basics/
#5

[eluser]Aishwarya.M.B[/eluser]
i've worked out on your code.....
i've edited the code.
just do the following changes...

ci/application/config/routes.php
$route['default_controller'] = "AccessDatabase";

system/application/config/database.php
$db['default']['database'] = "efo";

Controller:
AccessDatabase.php
&lt;?php

class AccessDatabase extends Controller{

function AccessDatabase()

{

parent::Controller();

}



function index()

{

$this->load->model('AccessDatabase_model');



$data['result'] = $this->AccessDatabase_model->getData();

$data['page_title'] = "CI Hello World App!";

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

}

}

?&gt;



Model

AccessDatabase_model.php
&lt;?php

class AccessDatabase_model extends Model {

function AccessDatabase_model()

{

parent::Model();

}

function getData()

{

$query = $this->db->get('sample');

if ($query->num_rows() < 0)

{

show_error('Database is empty!');

}

else{

return $query->result();

}

}



}

?&gt;


VIEW
AccessDatabase_view.php

&lt;html&gt;

&lt;head&gt;

&lt;title&gt;&lt;?=$page_title?&gt;&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;?php foreach($result as $row):?&gt;



&lt;?=$row->a?&gt;





&lt;?=$row->b?&gt;

&lt;?=$row->c?&gt;





&lt;?php endforeach;?&gt;

&lt;/body&gt;

&lt;/html&gt;



this code is working....
try this.
#6

[eluser]InsiteFX[/eluser]
Aishwarya.M.B,

As jedd mentioned please use code blocks for your code it helps a lot when others are viewing it!

Code:
Your code

Enjoy
InsiteFX
#7

[eluser]Aishwarya.M.B[/eluser]
i think a programmer can understand which is the code....
so no need of code blocks
#8

[eluser]alboyd[/eluser]
[quote author="Aishwarya.M.B" date="1252152983"]i think a programmer can understand which is the code....
so no need of code blocks[/quote]

Why be a smart arse? It's not a matter of understanding where code starts and stops - it's a matter of being able to better read the code because it is segregated and formatted appropriately.
#9

[eluser]jedd[/eluser]
[quote author="Aishwarya.M.B" date="1252152983"]i think a programmer can understand which is the code....
so no need of code blocks[/quote]

This may be true, but consider the context.

If you're asking for help, it is polite and more effective to provide as much useful information as possible. That includes making the information more readable. Most programmers these days use a colour-highlighting editor to programme in, consequently they're more familiar with seeing code as is shown inside [ code ] tags. Most question-askers post code from several different files, and at least one error message, so it's not just 'understanding' code, it's understanding which code goes with which type of file or error.

It takes very little effort for the poster of a message to write 'and here's my controller code ...' and then open a code block, close it when done and then do similarly with their model, their helper, their view, etc. It means each section really stands out and can be quickly identified by the reader.

Remember also that the poster : reader ratio is typically upwards of 1 : 100. This thread, for example, has had a 150 views. Going back to my original point - it's polite here to format it properly as it's a minute of the poster's time, but interpreting what the message actually means will take an additional 150 minutes (distributed). And it's effective to format it properly because it's very likely that if you post a lump of code that is difficult to read, it's not going to get the same love and attention from those 150 people as an easily readable question would. In other words, by being lazy and inconsiderate you'll get less help. Kind of like a scale model of Life.
#10

[eluser]Unknown[/eluser]
t looks like you’ve forgotten your constructor in your controller.




Theme © iAndrew 2016 - Forum software by © MyBB