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.

11
  • What does Line Number 124 say? Commented Mar 20, 2014 at 12:54
  • Try checking the port Commented Mar 20, 2014 at 12:54
  • Is your database named user? Commented Mar 20, 2014 at 12:55
  • The port is fine. I am able to connect with this port using my java application. Commented Mar 20, 2014 at 12:55
  • did you bind port with your localhost check this stackoverflow.com/questions/12795300/… Commented Mar 20, 2014 at 12:56

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

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

2 Comments

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?
@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.
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';

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.