Welcome Guest, Not a member yet? Register   Sign In
Unable to connect to database....help pleaseeee
#1

[eluser]Gaurav Kanojiya[/eluser]
Hi all,
I know so many of my friends has already raised this problem in this forum, but after going through those posts and after trying their fixes i decided to finally post my problem here. So here is my problem. I am getting this error :

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Filename: C:\xampp\htdocs\CodeIgniter\system\database\DB_driver.php

Line Number: 124

and here is my code.

/controllers/hello.php:

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

class hello extends CI_Controller
{
    public function index()
    {
        $this->load->model('hello_model');
        $data['records']=$this->hello_model->getAll();
        $this->load->view('you_view',$data);
    }
}
?>

/views/you_view.php:

Code:
<p>My view has been loaded.</p>
&lt;?php
foreach(result() as $row):
?&gt;
    <h1>&lt;?php echo $row->title; ?&gt;</h1>
&lt;?php
endforeach;
?&gt;

/models/hello_model.php:

Code:
&lt;?php

class hello_model extends model
{
    function getAll()
    {
    $q=$this->db->get('test'); // query where 'test' is table name.

        if($q->num_rows()>0)
        {
           foreach ($q->result() as $row)
           {
              $data[]=$row;
           }
        return $data;
        }
    }
}
?&gt;

autoload.php:

Code:
.
.
.

$autoload['libraries'] = array('database');


$autoload['helper'] = array('url');
.
.
.

database.php:

Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'ci_hello';
$db['default']['dbdriver'] = 'mysql';

config.php:

Code:
$config['base_url'] = 'http://localhost/codeigniter/index.php';

$config['index_page'] = 'index.php';

routes.php:
Code:
$route['default_controller'] = "hello";

PS: I have tried writing all my file names in smallcase, writing CI_Model,etc.....I am a newbie in CodeIgniter....and yes the version i am using is 2.1.3.
#2

[eluser]rochellecanale[/eluser]
Are you sure your database values are correct? Double check It. And let me know if it is correct or wrong.
#3

[eluser]rochellecanale[/eluser]
In your controller:
Code:
<p>My view has been loaded.</p>
&lt;?php
foreach(result() as $row):        //try to replace the result() to $records->result()
?&gt;
    <h1>&lt;?php echo $row->title; ?&gt;</h1>
&lt;?php
endforeach;
?&gt;

Let me know if it works.
#4

[eluser]Gaurav Kanojiya[/eluser]
Thnx for answering rochellecanale,
I hv made changes as per urs, but still i am getting that error.
I hv read in forums that class name should start with capital letter, i hv tried that also and evn tried routes.php with changed name and as per old name.I dont know where i am wrong. I hv double checked the database values also.Sad
#5

[eluser]Gaurav Kanojiya[/eluser]
Ok, my problem was with database password. I have not given any and their I hv written 'root'. But now I am getting a blank screen. Why is it so????
#6

[eluser]Gaurav Kanojiya[/eluser]
Ok now i have found the problem. Here I am posting so that anyone having the same problem can know what to do. I have done these changes in my index.php file in codeigniter folder. Simply change the 'if' for the 'ENVIRONMENT' with the below code.

Code:
.
.
.
if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'development':
            // Report all errors except E_NOTICE
            // This is the default value set in php.ini
            error_reporting(E_ALL ^ E_NOTICE);
            ini_set('display_errors', 1);  //this is the main line of code that makes to produce errors
            ini_set('ignore_repeated_errors', TRUE);
        break;

        case 'testing':
        case 'production':
            // Report all errors
            error_reporting(E_ALL);
            ini_set('display_errors', 0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
}
.
.
.

Cheers :coolsmile:
#7

[eluser]mohammad.khlouf[/eluser]
same thing after i did that,, its the same




Theme © iAndrew 2016 - Forum software by © MyBB