Codeigniter/Postgresql: Site works but cannot fetch data from db - php

I am using Postgresql database connection with codeigniter. I am new to postgresql. The site opens. But I cannot login nor fetch any values from the database.
I will upload the code for my config file as well as the database.php file.
config.php
// Keep as it is for same if MySQL server is running on same server
define("DBHOSTNAME",'ip_address');
// Add your database name here
define("DBNAME",'db_name'); // Here the database name is 'dummy_myecommerce'
// Add your database user name here
define("DBUSER",'username'); // Here the username is 'dummy_website'
// Add your database password here
define("DBPWD",'password');
Now I will upload the database.php file:
$db['default']['hostname'] = 'ip_address';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'db_name';
$db['default']['dbdriver'] = 'postgre';
$db['default']['dbprefix'] = 'pre_';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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;
$db['default']['port'] = 5432;
If anything else is required, please let me know. Thanks

Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might require a full DSN string to be provided. If that is the case, you should use the ‘dsn’ configuration setting, as if you’re using the driver’s underlying native PHP extension, like this:
// PDO
$db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
Helpful link - http://www.codeigniter.com/user_guide/database/configuration.html

Try this as well
$db['default']['hostname'] = 'pgsql:host=localhost;dbname=yourdb'; //set host
$db['default']['username'] = 'your username'; //set username
$db['default']['password'] = 'your password'; //set password
$db['default']['database'] = 'your database'; //set databse
$db['default']['dbdriver'] = 'pdo'; //set driver here

Related

unable to connect database : Codeigniter

I have noticed some unexpected behavior while connecting MySQL Codeigniter
Here is scenario:
-> I configured database library for autoload. (config/autoload.php)
$autoload['libraries'] = array('database', 'session');
-> But after configuring that, I can't open anything. I mean to say, Controller or View.
Have I done something wrong?
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'password';
$db['default']['database'] = 'myfamilyfirst';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = FALSE;
$db['default']['stricton'] = FALSE;
I tried to set $db['default']['autoinit'] = TRUE; to $db['default']['autoinit'] = FALSE;
Now I can open the pages but unable to connect the database.
Please help.
Probably you are using new xampp or wamp. So it will not support mysql.
Change this
$db['default']['dbdriver'] = 'mysqli'; # Change
In codeignator latest version we need to connect database using Mysqli. So in database file you need to give database driver as mysqli like this,
$db['default']['dbdriver'] = 'mysqli';
New codeignator version have mysqli dbdriver. If we have add mysql instated mysqli then shows following error.
Message: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
Correct way is : $db['default']['dbdriver'] = 'mysqli';
Please make sure that your database username and password matches. Sometimes, these little things matters a lot.

code igniter error: Unable to connect to your database server using the provided settings. Filename: core/Loader.php Line Number: 346

I'm developing a website application using codeigniter at localhost and then i host it in hawkhost.
At localhost it runs well, but at host there's and error :
Unable to connect to your database server using the provided settings.
Filename: core/Loader.php Line Number: 346
this is my db configuration
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '****';
$db['default']['password'] = '****';
$db['default']['database'] = '****';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;
I know many people already asked this problem but it doesn't seems to solve anything on my case.
changing $db['default']['db_debug'] = TRUE; to false only hide the error but when i tried to get data from query it doesn't return anything.
I also tried to change $db['default']['pconnect'] = FALSE; to true and still doesn't work.
Any help please?
Just change localhost to the local ipaddress i.e. 127.0.0.1 and it will start working.
$db['default']['hostname'] = 'localhost';
to
$db['default']['hostname'] = '127.0.0.1';
In my case setting pconnect to false worked.
$db['default']['pconnect'] = FALSE;
The problem is really due to credentials.
I test it using simple code to check if connection can be made.
The problem is in username, i realize there's additional suffix when using hosting. such as suffix_user
I think the same errors in Core/Loader.php Line Number: 346 has the same solution. make sure the username, password and host are right.
It's solved. Thank you everyone for helping.
Just change
$db['default']['hostname'] = 'localhost';
to
$db['default']['hostname'] = '127.0.0.1';
and voila.
Most of the time this problem happens when codeigniter is in live environment.
That’s why I assumed a domain name ‘(example: domainname.com)’ and after creating a mysql database (example: testDb) and user name (example: testUser) you will find a prefix name automatically added with your mysql database name and user name like ‘domainname_ testDb’ and ‘domainname_ testUser’.
Now in codeigniter /application/config/database.php edit following:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'domainname_ testDb ';
$db['default']['password'] = '*******';
$db['default']['database'] = 'domainname_ testUser';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'domainname_ testDb ';
$db['default']['password'] = '*******';
$db['default']['database'] = 'domainname_ testUser';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;
If anyone else is having this issue, try disabling SE-Linux if you have it enabled. It was causing the connection issues for me, with php 5.4.
Also worth checking is switching
$db['default']['dbdriver'] = 'mysql';
to
$db['default']['dbdriver'] = 'mysqli';
as mysql is depreciated after php 5.5.

Database Error Occurred

I am using CI and my database is on dream host. And when i connect database. it gives error like this
A Database Error Occurred
Unable to connect to your database server using the provided
settings.
Filename:
/home/demo_smartmobe/demos.smartmobe.com/nayacinema/webpart/third_party/MX/Loader.php
Line Number: 98
my ci code for database connection is
$db['default']['hostname'] = 'hostname';
$db['default']['username'] = 'username';
$db['default']['password'] = '​******';
$db['default']['database'] = 'database_name';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
username and password is correct.
and i had tried by simple php code and it works. Php Code is here.
$hostname = "mysql.demos.smartmobe.com"; // eg. mysql.yourdomain.com (unique)
$username = "nayacinema"; // the username specified when setting-up the database
$password = "****"; // the password specified when setting-up the database
$database = "nayacinema"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM TblUsers");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
what may be the problem? I had add MX folder in Ci folder for make it HMVC. And error is shown in /third_party/MX/Loader.php. IN localhost in works but in live there is error .please help me. Thank u
You stated your username and password are correct, but what about the hostname?
Modify your hostname setting to be your correct hostname.
E.g.
$db['default']['hostname'] = 'localhost';
or
$db['default']['hostname'] = '127.0.0.1';
You will also need to change $db['default']['database_name'] to match the name of the database you're attempting to use.
You need to get the correct database connection info from Dreamhost. You can find the ones that you have set up for that domain in your Dreamhost Web Panel, under the MySQL Databases tab, on the left side. Under that page, you can find the hostname, database, and username. If you click on the username that has access to the database, you can find out what your password is. Put those values into your database.php config file, like in the fields show below.
$db['default']['hostname'] = 'mysql.example.com';
$db['default']['port'] = "3306";
$db['default']['username'] = 'mysql_username';
$db['default']['password'] = '​mysql_password';
$db['default']['database'] = 'mysql_database';
First of all, if you are accessing the database within the same pc.
(localhost and 127.0.0.1 are the same)
If not, the host name should be identified by DNS Name or with domain name included.
And if the database port is not the default (3306), you should include it on the config.
Like the code in your php:
$db['default']['hostname'] = 'mysql.demos.smartmobe.com';
$db['default']['username'] = 'nayacinema';
$db['default']['password'] = '​******';
$db['default']['database'] = 'nayacinema';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
//$db['default']['port'] = 3307; //modify if the port is not the default 3306

Connect to IBM DB2 from Codeigniter

I have never worked with DB2 before. I am trying to connect from codeigniter. I have used the following settings in my database config file:
$db['default']['hostname'] = 'ipAddress';
$db['default']['port'] = 'portNumber';
$db['default']['username'] = 'uname';
$db['default']['password'] = 'pword';
$db['default']['database'] = 'myDBName';
//$db['default']['dbdriver'] = 'pdo';
$db['default']['dbdriver'] = 'odbc';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;
and it throws the following error:
Unable to connect to your database server using the provided settings.
Filename: C:\xampp\htdocs\archive\system\database\DB_driver.php
Again, I am totally new to DB2, so in case I need to install anything extra (I assume the driver is already included in my latest CI download) or I need to define a dsn (please let me know how to do that if i require that) please do let me know, will very much appreciate your help.
Yes, you need to install the IBM Data Server Client package, or at least Runtime Client. Download the appropriate driver from http://www-01.ibm.com/support/docview.wss?uid=swg27016878
The manual explains how to configure DB2 access from a PHP application.
You need to add the full connection to the hostname parameter , as the following code
$active_group = 'default';
$active_record = FALSE;
$db['default']['hostname'] = "driver={IBM db2 odbc DRIVER};Database=DBNAME;hostname=localhost;port=50000;protocol=TCPIP;" . "uid=username; pwd=password";
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'DBNAME.SCHEMANAME';
$db['default']['dbdriver'] = 'odbc';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;

Codeigniter Multiple Database Connection

I am using CodeIgniter and I am trying to access a database that is placed remotely on another server. I can access that server remotely but I can't connect via my MySQL Connection settings. Here are my MySQL settings:
$db['default']['hostname'] = 'myhostname';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'MyDb';
$db['default']['dbdriver'] = 'mysqli';
$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;
I am not sure where I am making a mistake so if someone can point out the mistake it will be really helpful.
First, check username and password for mysql (obviously username cannot be root and password cannot be blank for security reasons) and for hostname, you might have to supply IP address of your server where mysql is hosted. Rest configurations appear to be fine. You can ask for these information from your hosting company.

Categories