Codeigniter error: Call to undefined function mysql_pconnect() - php

I have updated my codeigniter version from 2.2.4 step by step to 3.0.6 and I get an error:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function mysql_pconnect()
Filename: path-to-project\system\database\drivers\mysql\mysql_driver.php
Line Number: 135
Backtrace:
File: path-to-project\application\controllers\Main.php
Line: 10
Function: __construct
File: path-to-project\index.php
Line: 315
Function: require_once
I have just replaced my index.php file and system directory with the new one and made some changes in my application according to tutorial.
and this is the Main controller:
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
}
What causes the problem?!
And this is the link of the tutorial.

Deprecated features in PHP 5.5.x:
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.
You're using the deprecated mysql dbdriver. Locate the config/database.php file and change dbdriver to use mysqli :
$db['default']['dbdriver'] = 'mysqli';

Thanks to Anant
I come to a conclusion:
I completely changed my old database.php file in config folder with the new one:
From:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = '';
$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;
To:
$db['default'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'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
);
And the error is gone!

go to application/config/database.php
and just change mysql to mysqli
like
this was before:
$db['default']['dbdriver'] = 'mysql';
this was after solution:
$db['default']['dbdriver'] = 'mysqli';
i just change mysql to mysqli. that's it
my error was
Fatal error: Uncaught Error: Call to undefined function
mysql_pconnect() in
E:\manish_data\software\xampp\htdocs\ci2\system\database\drivers\mysql\mysql_driver.php:92
Stack trace: #0
E:\manish_data\software\xampp\htdocs\ci2\system\database\DB_driver.php(116):
CI_DB_mysql_driver->db_pconnect() #1
E:\manish_data\software\xampp\htdocs\ci2\system\database\DB.php(149):
CI_DB_driver->initialize() #2
E:\manish_data\software\xampp\htdocs\ci2\system\core\Loader.php(347):
DB(Array, NULL) #3
E:\manish_data\software\xampp\htdocs\ci2\application\models\usermodel.php(20):
CI_Loader->database() #4
E:\manish_data\software\xampp\htdocs\ci2\application\controllers\users.php(15): UserModel->getUsers() #5
E:\manish_data\software\xampp\htdocs\ci2\system\core\CodeIgniter.php(360):
Users->index() #6
E:\manish_data\software\xampp\htdocs\ci2\index.php(202):
require_once('E:\manish_data\...') #7 {main} thrown in
E:\manish_data\software\xampp\htdocs\ci2\system\database\drivers\mysql\mysql_driver.php
on line 92

If this error happened when you're hosting the website, make sure to set the correct PHP Version (the one your CI use).

Related

How to connect localhost database to another server database and insert form value using codeigniter? [duplicate]

This question already has an answer here:
Datebase error mysqli::real_connect(): (HY000/2002): Connection refused codeingiter
(1 answer)
Closed 2 years ago.
<?php
$active_group = 'default';
$query_builder = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'campus';
$db['default']['password'] = 'Mac#123';
$db['default']['database'] = 'campusnew';
$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'] = TRUE;
$db['otherdb']['hostname'] = "101.51.150.42";
$db['otherdb']['username'] = 'software';
$db['otherdb']['password'] = 'Lmsnew';
$db['otherdb']['database'] = 'lmsnew';
$db['otherdb']['dbdriver'] = "mysqli";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$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'] = TRUE;
model function
public function newusersignin()
{
$DB2 = $this->load->database('otherdb', TRUE);
$firstName=$this->input->post('firstName');
$lastName=$this->input->post('lastName');
$timezone=$this->input->post('timezone');
$fullname=$firstName.' '.$lastName;
$email=$this->input->post('email');
$phone=$this->input->post('phone');
$ip_address=$this->input->ip_address();
$institutional=$this->input->post('institutional');
$pass=$firstName.mt_rand(1,100);
$username=$email;
$password=$this->hash($pass);
$insertnewuser= array('name' =>$fullname,'email'=>$email,'phone'=>$phone,'username'=>$username,'password'=>$password,'usertype'=>'ClgAdmin','create_date'=>date('y-m-d H:i:s'),'create_userID'=>1,'create_username'=>'campus','systemadminactive'=>0,'timezone'=>$timezone,'status'=>1 );
$this->db->insert('admin',$insertnewuser);
$DB2->insert('admin',$insertnewuser);
}
In this question I am simply create two database connection one is for localhost and another one is IP which is hosted on different server. Now, what happen when I am going to insert form data in my IP Server then it throw an error and i.e.
Message: mysqli::real_connect(): (HY000/2002): Connection refused
I don't know why this happen? How can I solve this problem? Please help me.
Thank You
Instead of doubling everything in two arrays, can you check against the URL?
Just change the localhost to be more specific as to which db you need to connect too
$is_local = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";
if (strpos($is_local, 'localhost') !== false) {
// LOCAL
$db_name = "db_name";
$db_user = "root";
$db_pass = "root_pass";
$db_host = "localhost";
} else {
// OTHER
$db_name = "db_name_2";
$db_user = "root_live";
$db_pass = "root_pass_live";
$db_host = "mysql.live.com";
}
Then your connection code is always the same from then
Database Configuration
CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database.php. You can also set database connection values for specific environments by placing database.php in the respective environment config folder.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array()
);
Inserting Data
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->insert('mytable', $data);
Your database configuration and methodology given above are correct. All you have to do is allow remote connection for your database on your server. If you are using CPanel, you can browse to "Remote MySQL" and add access host. Add your ISP IP address to allow only your computer or "%" for any computer to access.

How to connect Codeigniter with MSSQL (SQL Server)?

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/

Codeigniter not able to connect to firebird

I have seen and tried all the solutions in Codeigniter - multiple database connections, You have specified an invalid database connection group codeigniter error, firebird - codeigniter connection and none of them worked.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'testing';
$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;
$db['fbird']['hostname'] = "localhost";
$db['fbird']['username'] = "SYSDBA";
$db['fbird']['password'] = "masterkey";
$db['fbird']['database'] = "C:\waitkyl.fdb";
$db['fbird']['dbdriver'] = "firebird";
After this configuration, I call the fbird database this way:
class Fb_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function getAll(){
$db = $this->load->database('fbird', TRUE);
return $db->get('categories')->result();
}
}
And the error I get after trying output the values print_r($this->fb_model->getAll()); is:
Fatal error: Call to a member function result() on a non-object
That usually means that the table 'categories' doesn't exists..which isn't true.
So I have tried to change my configuration file into:
$db['fbird']['hostname'] = "localhost";
$db['fbird']['username'] = "SYSDBA";
$db['fbird']['password'] = "masterkey";
$db['fbird']['database'] = "C:\waitkyl.fdb";
$db['fbird']['dbdriver'] = "firebird";
$db['fbird']['dbprefix'] = "";
$db['fbird']['pconnect'] = FALSE;
$db['fbird']['db_debug'] = TRUE;
$db['fbird']['cache_on'] = FALSE;
$db['fbird']['cachedir'] = "";
$db['fbird']['char_set'] = "utf8";
$db['fbird']['dbcollat'] = "utf8_general_ci";
And also removed from the php.ini file the ; from the following lines:
extension=php_pdo_firebird.dll
extension=php_interbase.dll
And if I refresh the page now the error I receive is:
Unable to connect to your database server using the provided settings.
Filename: C:\xampp\htdocs\projtesting\system\database\DB_driver.php
Line Number: 124
In order to see if the credentials from the database were fine I opened it on the software SQL Manager 2008 Lite for Interbase and Firebird, and it was ok.
Any ideas of what's going wrong?
Solution: Codeigniter 3 (dbdriver = ibase)
$db['firebird'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'sysdba',
'password' => 'masterkey',
'database' => 'c:/database.GDB',
'dbdriver' => 'ibase',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'ANSI',
'dbcollat' => 'NONE',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Codeigniter Not Connecting to SQL Server

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.

Codeigniter: Using PDO instead of mysql

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.

Categories