Welcome Guest, Not a member yet? Register   Sign In
Shopping Cart Class - A Database Error Occurred
#1

[eluser]alan-tucker[/eluser]
When posting a form to my controller page (action="/basket/add_to_cart/") I get a
A Database Error Occurred - Unable to connect to your database server using the provided settings.
error.

Any ideas or suggestions?

My Controller

Code:
<?php
class Basket extends Controller {
    
    function __construct()
    {
        parent::Controller();
        $this->load->library('cart');
    }    
    
                
    function index()
    {
        $this->load->view('basket');    
    }
    
    function add_to_cart()        
    {    
        // add the selected product to the cart
    $data = array(
               'id'      => 'sku_123ABC',
               'qty'     => 1,
               'price'   => 39.95,
               'name'    => 'T-Shirt',
               'options' => array('Size' => 'L', 'Color' => 'Red')
            );
        
        $this->cart->insert($data);                
        redirect('/');
    }    
    
}
?>

#2

[eluser]InsiteFX[/eluser]
Have you loaded the database Library?

Did you setup your user details in application/config/database.php

I see nowhere in your code the the database library is loaded.

this is saying that your configuration for the database is wrong!
A Database Error Occurred - Unable to connect to your database server using the provided settings.

Enjoy
InsiteFX
#3

[eluser]alan-tucker[/eluser]
Thanks for the reply, I am just starting out (again) with CI.

I have my config details in application/config/database.php as well as my session info in application/config/config.php

The Shopping Cart Class does not mention about the database library.

Thanks

Alan
#4

[eluser]bretticus[/eluser]
[quote author="alan-tucker" date="1253133594"]
I have my config details in application/config/database.php as well as my session info in application/config/config.php

The Shopping Cart Class does not mention about the database library.[/quote]

You have to auto load the database class in the application/config/config.php file before it will connect automatically. See auto-loading in the manual. This is covered the the blog videos on the website (but more in depth in the manuals.) See Saving Session Data to the Database.
#5

[eluser]SitesByJoe[/eluser]
And be sure to make your database table! ;-)
Code:
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id)
);
#6

[eluser]alan-tucker[/eluser]
Thanks for the replies :-)

@bretticus - auto db connect throws an error when i use $autoload['libraries'] = array('database'); - A Database Error Occurred Unable to connect to your database server using the provided settings.


@SitesByJoe - Yes I have created the table ;-)


Thanks once again

Alan
#7

[eluser]InsiteFX[/eluser]
If your still getting a database error then something is wrong with your connect parameters!

Show us your dataqbase.php config file that seems to be were the problem is.

Enjoy
InsiteFX
#8

[eluser]bretticus[/eluser]
[quote author="alan-tucker" date="1253142020"]
@bretticus - auto db connect throws an error when i use $autoload['libraries'] = array('database'); - A Database Error Occurred Unable to connect to your database server using the provided settings.[/quote]

Yep, make sure that your connection credentials are good. Make sure you have created the user you are using in the config file. Make sure you have set the password, etc for that user.

Unfortunately, CI doesn't return the error that the database driver returns (probably because they are non standard accross different databases.) You could try removing the @ operator from the mysql functions in system/database/drivers/mysql/mysql_driver.php to get a better error message.

In other words, temporarily change this line:

Code:
return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);

to this line:

Code:
return mysql_connect($this->hostname, $this->username, $this->password, TRUE);
#9

[eluser]alan-tucker[/eluser]
My config database.php

Code:
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "alantuc1_dbuser";
$db['default']['password'] = "shopper";
$db['default']['database'] = "alantuc1_shop";
$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";
#10

[eluser]alan-tucker[/eluser]
Have been onto my host and it seems that mysql_pconnect has been disabled on the server. So changing to $db['default']['pconnect'] = FALSE; seems to work, well the session is going into the database.

Thanks for all your help and suggestions.


Alan




Theme © iAndrew 2016 - Forum software by © MyBB