1

I am new to PHP and CodeIgniter and I am trying to fetch data from a mysql table using MVC pattern of codeIgniter.

My model class is:

<?php

class News_Model extends CI_Model
{

    public function _construct()
    {
        $this->load->database();
    }

    public function get_news($id)
    {
        if($id!=FALSE)
        {
            $query= $this->db->get_where('news', array('id' => $id));
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }   
    }

}


 ?>

and my database.php file is:

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

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'user';
$db['default']['dbdriver'] = 'mysql';
$db['default']['port'] = '3306';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

But when I run my file, it shows the following error:

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

Filename: C:\wamp\www\codeignite\system\database\DB_driver.php

Line Number: 124

My mysql is up and running but still i am not able to figure out the cause for this error.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
AppleBud
  • 1,501
  • 8
  • 31
  • 47

5 Answers5

8

Just for debugging your problem:

Go to system/database/mysql/mysql_driver and in db_connect method delete the @ from @mysql_connect($this->hostname, $this->username, $this->password, TRUE);

That will show you what is wrong with the ddbb connection, and you'll now what is the problem. After that, post the MySQL server error.

Federico J.
  • 15,388
  • 6
  • 32
  • 51
1

First debug your database connection using this script at the end of database.php

echo '<pre>';
print_r($db['default']);
echo '</pre>';

Or you can change

$db['default']['pconnect'] = TRUE; --> $db['default']['pconnect'] = FALSE;

in /application/config/database.php

sotoz
  • 3,100
  • 3
  • 34
  • 41
Fortran
  • 593
  • 4
  • 14
  • 2
    1) It's hardcoded, printing the array won't do any debug, nothing is going to change those hardcoded values; 2) if you can't even connect, what's the point of setting on or off a permanent connection? – Damien Pirsy Mar 20 '14 at 13:09
  • @DamienPirsy I think it has to do with the DB location/pooling. My local SQL Express in a demo project seemed to fail with Codeigniter, but worked on a direct connection string. Setting pconnect to False cleared it up. – JBC Oct 19 '14 at 05:21
1

Don't know if it is a copy paste error or it really matters on your problem but you have only one underscore at the model constructor.

You should try it like that

 public function __construct()

I know this could be a comment but I can't comment (yet).

sotoz
  • 3,100
  • 3
  • 34
  • 41
1

Please set username and password for file database.php on hosting, it's ok.

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'abaccc';
$db['default']['password'] = 'abaccc123';
$db['default']['database'] = 'abaccc';
$db['default']['dbdriver'] = 'mysql';
fedorqui
  • 275,237
  • 103
  • 548
  • 598
lulu
  • 21
  • 2
1

Just run your mysql server. i have an error like that. and i run my mysql server, if you are already installed it, please go to services -> mysql/mysqld/mysql2/

or anything like mysql.. select automatic and start.

Jimbo
  • 49
  • 8