Why this happen? How can i solve it? - codeigniter

Severity: 8192
Message: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
Filename: mysql/mysql_driver.php
Line Number: 136
Backtrace:
File: C:\wamp\www\ci\application\controllers\c_testing.php
Line: 27
Function: __construct
File: C:\wamp\www\ci\index.php
Line: 315
Function: require_once
in my codeigniter file in databease.php i use this
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:81',
'username' => 'root',
'password' => '1234',
'database' => 'testing',
'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
);
then in my testing.php file
Class Testingdata extends CI_Model{
function __construct(){
parent::__construct();
$this->load->database();
$this->db->reconnect();
}
// data obtained from database
function get_data(){
$this->db->select('*');
$this->db->from('lecturer_profile');
$query = $this->db->get();
return $query->result();
}
}
?>`enter code here`

You need to change the database config. Go to the following path in your project /application/config/database.php find the dbdriver setting and make sure it's mysqli.

Related

mysqli::real_connect(): (HY000/1045): Access denied for user [duplicate]

I'm new to Codeigniter PHP framework. When I'm testing my application I get 'Unknown database db_name' error. I have browsed through several sites but didn't found solution for the problem as I'm trying the same to connect with wamp's mysql database. Any help would be appreciable.
Following is database.php in config folder: image describing Test database:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
'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()
);
I was getting same error when i installed new wampserver 3.2.0, it is an evil it installs MariaDB by default and port 3306 is assigned to it , you cannot even change it. I think codeigniter/php tries to connect to this port by default.
I used following , it worked for me i.e. hostname => 'localhost:3308'
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:3308',
'username' => 'root',
'password' => '',
'database' => 'soft',
'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
);
That's what i did to handle whether db exists or not :
First Change this in application/config/database.php
'db_debug' => (ENVIRONMENT !== 'production')
to
'db_debug' => FALSE;
Then add these two in your controller
$this->load->dbforge();
$this->load->dbutil();
Then check it in your method
if( $this->dbutil->database_exists($this->db->database))
{
echo 'Database Already Exists';
}
else
{
if($this->dbforge->create_database($this->db->database))
{
echo 'Database created successfully !';
}else
{
print_r($this->db->error());
/*
Array (
[code] => 1007
[message] => Can't create database 'my_db'; database exists
)
*/
}
}
I was too getting this error. There are 2 fields in application/config/database.php file that should match up with your actual database:
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
Make sure, the actual DB is of the same type & char_set as mentioned in the above file.

Infinityfree.net and codeigniter

Good Day!
I have this problem using infinityfree.net as my hosting site.
I used codeigniter framework for my code.
The problem is:
An uncaught Exception was encountered
Type: RuntimeException
Message: Unable to locate the model you have specified: Login_model
My code working properly on xampp.
database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'HOSTNAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE_NAME',
'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
);
Here's my login_model.php
class Login_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
function login($par1,$par2){
//CODE HERE
}
Here's my controller
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('url', 'form');
}
public function login()
{
$this->load->model('login_model');
}
PS. i didnt include much more code in controller because thats the only thing that has error and also Im unable to post too much code because of the rule of stackoverflow. Hoping you guys to help me. Thanks in advance
Fixed
I just renamed my login_model to Login_model

Codeigniter PDOException: SQLSTATE[HY000] [1049] Unknown database [duplicate]

I'm new to Codeigniter PHP framework. When I'm testing my application I get 'Unknown database db_name' error. I have browsed through several sites but didn't found solution for the problem as I'm trying the same to connect with wamp's mysql database. Any help would be appreciable.
Following is database.php in config folder: image describing Test database:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
'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()
);
I was getting same error when i installed new wampserver 3.2.0, it is an evil it installs MariaDB by default and port 3306 is assigned to it , you cannot even change it. I think codeigniter/php tries to connect to this port by default.
I used following , it worked for me i.e. hostname => 'localhost:3308'
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:3308',
'username' => 'root',
'password' => '',
'database' => 'soft',
'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
);
That's what i did to handle whether db exists or not :
First Change this in application/config/database.php
'db_debug' => (ENVIRONMENT !== 'production')
to
'db_debug' => FALSE;
Then add these two in your controller
$this->load->dbforge();
$this->load->dbutil();
Then check it in your method
if( $this->dbutil->database_exists($this->db->database))
{
echo 'Database Already Exists';
}
else
{
if($this->dbforge->create_database($this->db->database))
{
echo 'Database created successfully !';
}else
{
print_r($this->db->error());
/*
Array (
[code] => 1007
[message] => Can't create database 'my_db'; database exists
)
*/
}
}
I was too getting this error. There are 2 fields in application/config/database.php file that should match up with your actual database:
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
Make sure, the actual DB is of the same type & char_set as mentioned in the above file.

Failing to connect to database

When I run my localhost, I get these errors
A PHP Error was encountered Severity: Warning
Message: mysqli::real_connect(): (HY000/2002): A connection attempt
failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond.
Filename: mysqli/mysqli_driver.php
Line Number: 203
Backtrace:
File: C:\xampp\htdocs\ciblog\application\models\Post_model.php Line: 4
Function: database
File: C:\xampp\htdocs\ciblog\index.php Line: 315 Function:
require_once
A Database Error Occurred Unable to connect to your database server
using the provided settings.
Filename: C:/xampp/htdocs/ciblog/system/database/DB_driver.php
Line Number: 436
I already tried to change the hostname to "mysql.hostingprovider.com:3306" but still get the error:
database.php file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysql.hostingprovider.com:3306',
'username' => 'root',
'password' => '123456',
'database' => 'posts',
'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
);
Post_model.php file
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_posts($slug = FALSE){
if($slug === FALSE){
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
}
post.php file
<?php
class Posts extends CI_Controller{
public function index (){
$data['title'] = 'Latest Posts';
$data['posts'] = $this->post_model->get_posts();
print_r($data['post']);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
}
Change hostname:
*$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
............*

unable to connect to database using the provided settings my error

i have this project created by php and mysql but i have error
Filename: C:\xampp\htdocs\demo\system\database\DB_driver.php
Line Number: 124
and uploaded my project to solve this with very thanks for all team .
http://www.webchinupload.com/f/2019-05/ebae9eaf2a9655eb442da8a9e648da3e.rar
my error in line 124
$this->conn_id = ($this->pconnect == false) ? $this->db_connect() : $this->db_pconnect();
// No connection resource? Throw an error
if ( ! $this->conn_id)
{
log_message('error', 'Unable to connect to the database');
if ($this->db_debug)
{
$this->display_error('db_unable_to_connect');
}
return FALSE;
}
Change hostname to localhost
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = ''; #database password
$db['default']['database'] = 'e-frosh_test';
Check your application/config/database.php
In codeigniter framework
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',//Put Your db passwaord
'database' => 'e-frosh_test',
'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
);

Resources