I'm using PHP 5.3.3 because I do not control the server. I'm also using CodeIgniter 3.1.0. I'm trying to use CI to connect to an Oracle server, Oracle 11g release 11.2.0.4.0.
I can connect just fine using php's oracle_connect:
$connect_string = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = HOSTNAME)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SID = MYSID)))";
$oconnect = oci_connect(USERNAME, PASSWORD, $connect_string );
$query = "select * from DB.TABLE";
$stid=oci_parse($oconnect, $query);
$r = oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
print_r($row);
}
But when I try to set up the db connection from within CodeIgniter, I get an error that the TNS name is not known, or that it can't connect, or that the service is unknown. Depends on how I tweak it.
Note that the examples on CodeIgniter may be wrong. The dsn uses SID= where the examples have SERVICE=; it wasn't until I read the user comments in the php manual that they suggested SID= for php's oci_connect.
Here's what I have for CodeIgniter:
$dsn_string = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = HOSTNAME)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SID = MYSID)))";
$db['default'] = array(
'dsn' => $dsn_string,
'hostname' => 'HOSTNAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DB',
'dbdriver' => 'oci8',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Any ideas about what may be happening? I'm going to try a few other things as well. I could just ignore CI's db connection, but since I'm using it mainly as an api provider for an angular front end, the db is the biggest reason I'm even using CI.
Thanks!
#Tpojka - I have to use the server I'm given, but turns out this wasn't the issue.
The problem was the .htaccess file. I used an existing .htaccess and it had the following line:
DirectoryIndex index.php
I didn't think anything of it at first since that's a pretty normal line, but then I realized that I had changed my config.php to remove index.php from the url. As soon as I removed that line from .htaccess everything started working again.
Sheesh. I didn't think that could result in database errors, but that was the ultimate fix to get rid of the last of the errors.
Related
We want to connect one of too many databases as a default.
as per user requriment so we want take a database name in a variable or Global variable.
we are unable to use any variable in Database.php file
so guys help me for this problem.
view code here Codeigniter 4.0.0
It has been a while since I tried to do "weird" things like attempting to assign dynamic variables in configuration files so I'm not entirely sure if CodeIgniter supports it. However, even if it does, I would suggest using the function designed for these sort of things:
https://codeigniter4.github.io/userguide/database/connecting.html#connecting-with-custom-settings
$custom = [
'DSN' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
$db = \Config\Database::connect($custom);
I suppose, since all else is the same except for database name you could also access the database configuration property for your default database and use $custom = array_replace($configDb, ['database' => $_SESSION['db_name']) to change out the database name. Obviously check that db_name exists and isn't empty and all that jazz. You could even put this entire logic in to a class with db_name as a constructor so you don't have to keep doing this all the time.
E.g. (untested):
namespace App\Config;
use InvalidArgumentException;
class DatabaseChooser extends \Config\Database
{
protected $dbName;
public function __construct(string $dbName = '')
{
$this->dbName = $dbName;
$config = $this->buildArr();
return parent::connect($config);
}
private function buildArr()
{
if (empty($this->dbName)) {
throw InvalidArgumentException('dbName cannot be empty');
}
$default = $this->defaultGroup;
if (property_exists($this, $default) && is_array($this->$default)) {
return array_replace($this->$default, ['database' => $this->dbName]);
} else {
throw new Exception('Invalid configuration.');
}
}
}
I'm sure you've considered it, but CI4 isn't exactly production ready afaik. To be honest, I'd be using something like Laravel, Lumen, or Symfony which already have an extensive and tested codebase, as well as many compatible third party packages.
My server use Windows Server with MSSQL 2012. While I am use OS X (El Capitan) with XAMPP (Apache) for Mac and develop website using Codeigniter 2.2.0.
Here is my configuration :
$active_group = 'my_mssql';
$active_record = TRUE;
$db['my_mssql']['hostname'] = 'xx.xx.xx.x';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = 'queue_sys';
$db['my_mssql']['dbdriver'] = 'mssql';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = TRUE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;
but the results is :
Are my settings be wrong?
I just want to be able to connect to that server. Does anyone have any advice on solving this?
it will be helpful for someone
Detail steps for How to connect SQL Server with PHP in WampServer
step 1)
download the appropriate driver based on your php version, check using php_info() function, for me 5.6, so the appropriate driver is SQLSRV30, found in the following link
https://www.microsoft.com/en-us/download/details.aspx?id=20098
step 2) extract the drives in C:\wamp\bin\php\php5.6.19\ext
focus on the following .dll file they should be there, otherwise we can't connect with SQL, these are
php_sqlsrv_56_nts.dll and php_sqlsrv_56_ts.dll
step 3) enable the drives in php.ini
as follow, which is found in C:\wamp\bin\apache\apache2.4.18\bin as follow
extension=php_sqlsrv_56_ts.dll
extension=php_sqlsrv_56_nts.dll
step 4) go to DB_driver.php line 96
found in
C:\wamp\www\public\system\database\DB_driver.php
please replace the mysqli by sqlsrv
public $dbdriver = ' mysqli ';
public $dbdriver = 'sqlsrv';
step 5) last and most important part, go to CI database config
in database.php file which's found in C:\wamp\www\public\application\config\database.php
adjust the params accordingly...
$db['default'] = array(
'dsn' => '',
'hostname' => 'ip address for sql server,port', // it should be SQL TCP enabled and firewall permitted for SQL port, whether default or custom.
'username' => 'your user name here',
'password' => 'your pwd here',
'database' => 'your db here',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'autoinit' => TRUE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
happy coding...
You may want to try odbc driver (built-in db driver in PHP). Mssql driver doesn't come convenient in PHP.
I got this working with CI 2.2.0 connected to MSSQL 2014 database (all Windows platform). I've also tried this using MSSQL 2012 before.
$active_group = 'my_mssql';
$active_record = TRUE;
$db['my_mssql']['hostname'] = 'Driver={SQL Server Native Client 11.0};Server=Host\Instance;Database=queue_sys;';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = '';
$db['my_mssql']['dbdriver'] = 'odbc';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = FALSE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;
Note:
SQL Server Native Client 11.0 or SQL Server Native Client 10.0, just play with both settings.
Server=xx.xx.xx.x usually is in the format Server=Host\Instance
The error is showing that SQLSRV driver implies the DLL is not loading ever.
check your php_info()
The SQLSRV driver requires for it.
check this for step by step solution
https://futbolsalas15.wordpress.com/2014/02/23/7-steps-to-make-sql-server-and-codeigniter-works/
Our site is on Rackspace cloud sites and is currently running fine with Codeigniter 3.0.0 on PHP 5.4.1.0 and connecting to a MSSQL 2012 database. Rackspace is in the process of upgrading PHP from 5.4.1.0 to 5.6.7-1 and has given testlinks for testing our websites in the new PHP environment. In this test environment, the site does not connect to the MSSQL database. However - no errors are thrown (that I can find, anyway!)
This is what I have for my database configuration:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mssqlXXXX.XXX',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE NAME',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
As a test, Rackspace also had me write a test controller. It also fails in Codeigniter. Code follows:
$username = "USERNAME";
$password = "PASSWORD";
$hostname = 'mssqlXXXX.XXX';
$dbname = "DATABASE";
//connection to the database
$dbcon = mssql_connect($hostname, $username, $password)or die("Unable to connect to MSSQL");
echo "Connected to MSSQL";
//select the database
mssql_select_db($dbname, $dbcon);
//SQL Select statement
$sqlselect = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
//Run the SQL query
$sqlquery = mssql_query($sqlselect);
//Output the query results
while ($result = mssql_fetch_array($sqlquery) )
{
echo "<br>";
print_r($result);
echo "<br>";
}
If I take this script out of the Codeigniter environment, it works.
I have tried changing my codeigniter database configuration as follows: hostname to an IP address instead of the Rackspace recommended value, changing the dbdriver to sqlsrv, using a dsn string instead of hostname and database and using the odbc database driver. Nothing works.
I am open to any and all suggestions.
EDIT
-------------------------------------------------------------------------
Rackspace says "issue with freetds, which is a library used my PHP's mssql module for connecting to MSSQL Databases; the issue appears to revolve around using passwords longer than 30 characters."
So - I shortened my password. Now the test controller mentioned above works! Hurrah! BUT - the standard Codeigniter database connection does not.
The issue was with Rackspace and they have corrected it.
I'm trying to connect to an SQL server using CodeIgniter. If I use the sqlsrv driver - I get a fatal error message and if I use the odbc driver - I get an 'Unable to connect to your database server using the provided settings error message. Does anyone know how to fix this problem??? I don't mind how, I just want Codeigniter to connect to the SQL Server Database.
This is the database config file
$db['otherdb']['hostname'] = '195.234.10.55\SQLEXPRESS';
$db['otherdb']['username'] = 'username';
$db['otherdb']['password'] = 'password';
$db['otherdb']['database'] = 'ONEDB';
$db['otherdb']['dbdriver'] = 'odbc'; // Done this in both ODBC and SQLSRV
$db['otherdb']['dbprefix'] = '';
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = TRUE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = '';
$db['otherdb']['char_set'] = 'utf8';
$db['otherdb']['dbcollat'] = 'utf8_general_ci';
$db['otherdb']['swap_pre'] = '';
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
Thanks
(Don't mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search...)
A short step-by-step guide to connect to SQL_server from CodeIgniter 3 using native driver:
First of all download PHP for SQL_server Microsoft native driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098. Take care to choose the one that fits your setup.
Install it to the PHP extension directory.
Configure the new extension in PHP.ini. uncommenting or appending the line (depending on the choosen driver version). I.e.:
extension = php_sqlsrv_55_ts.dll
Restart Apache.
Now your PHP setup is able to connect to SQL_server databases.
Set up your CodeIgniter database connection by configuring application/config/database.php like
<?php
// native-driver connection
$db['default'] = array(
'dsn' => '',
'hostname' => 'SERVER\SQLEXPRESS',
'username' => 'test_user',
'password' => 'test_password',
'database' => 'test01',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => 'application/cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Tested from CodeIgniter 3.0.1 on SQL_server 2008 R2 and SQL_server 2014 express.
Until Codeigniter implements the use of PDO, is there a way to use hack it into CI that's stable and secure? Currently, instead of using the db driver, I'm using a model instead which has all my PDO code like prepare, fetch, execute, etc. in it. What are the rest of you doing?
On CodeIgniter 2.1.4+ using MySQL databases (Edit the file: /application/config/databases.php).
To use PDO:
$db['default']['hostname'] = 'mysql:host=localhost';
$db['default']['dbdriver'] = 'pdo';
To use MySQLi
$db['default']['hostname'] = 'localhost';
$db['default']['dbdriver'] = 'mysqli';
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'mysql:host=127.0.0.1; dbname=****yourdatabasename*****; charset=utf8;',
'hostname' => '',
'username' => 'root',
'password' => '******yourpassword*******',
'database' => '',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Using PDO drivers instead of mysql require to change the hostname and the dbdriver like this :
$db['default']['hostname'] = 'mysql:host=localhost';
$db['default']['dbdriver'] = 'pdo';
CI, if used correctly, is both reliable and safe. Using PDO, while better if you are not using a framework, doesn't necessarily benefit you terribly over the CI_Database class.
If it really bothers you, you can swap out the mysql_*() functions for the equivalent mysqli_*() functions, but it really won't provide any discernable difference unless you are hyper-optimizing.
It should be noted that this can actually be done automatically by setting the dbtype appropriately (as Rocket notes below).
try php-activerecord I believe this use's PDO driver, its a simple plug and play via sparks.
Just a follow up to anyone having this same issue (including my future self), please ensure that whichever dbdriver you are loading has the correct library loaded in php.ini
$db['default']['dbdriver'] = 'mysqli'; //MySQLi <-- mysqli.dll
$db['default']['dbdriver'] = 'mysql'; //MySQL <-- mysql.dll
$db['default']['dbdriver'] = 'pdo'; //PDO <-- pdo.dll
Failure to load the correct dll will cause CodeIgniter to fail with a blank page.