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',
............*
Related
I got in trouble with Codeigniter 3 using the form validation library; I have to check that the email address submitted by a user into aregistration form is unique into the users database.
I use two databases in my project, the users one is not the default.
To perform the email check I use the following code:
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.email]');
I got an error about the missing users table into the default database, so I've realized that CI 3 looks the email to check into a default database ... not the correct users database, even if in the construct I load the correct model/database.
Is there a way to perform the check into a different database using the form validation above?
Thanks for any feedback
UPDATE
Below the code I use to load the model in the controller
function __construct()
{
parent::__construct();
$this->load->model("admin/user_model","user");
}
Below the code of the User_model
// Database
private $auth_db;
// Tables
private $table_users = 'users';
public function __construct()
{
parent::__construct();
$this->auth_db = $this->load->database('auth', true);
}
and...finally...in the config file the database configuration
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'xxxxxxxxx',
'password' => 'xxxxxxxxx',
'database' => 'vfr_main',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_unicode_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['auth'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'xxxxxxxxxxx',
'password' => 'xxxxxxxxxxx',
'database' => 'vfr_auth',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_unicode_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
In CodeIgniter 3, models do not have a direct way to control which database group they connect to. In this case I do not think we can simply write a custom validation rule, because is_unique is the only rule that makes a db call, and there is no built in way to change db.
In this case I think the most direct approach would be to extend the form validation class, and then add a new is_unique_authdb method in this extended library for checking with the second db. Then you would use this new method in place of the call you have shown above.
In the 3.x repo on git hub I see this is the existing method.
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return isset($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
And your extra method could be something like:
public function is_unique_authdb($str, $field)
{
$db = $this->CI->load->database('auth', true);
sscanf($field, '%[^.].%[^.]', $table, $field);
return isset($db)
? ($db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
Extending a native library is very simple. For example, to extend the native Form_validation class you’ll create a file named application/libraries/MY_Form_validation.php, and declare your class with:
class MY_Form_validation extends CI_Form_validation {
public function is_unique_authdb($str, $field)
{
$db = $this->CI->load->database('auth', true);
sscanf($field, '%[^.].%[^.]', $table, $field);
return isset($db)
? ($db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
}
Then you would load the library as normal but it will load your extended library and you can use the authdb method for this validation.
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique_authdb[users.email]');
CI3 docs for extending a native library.
Note: With CodeIgniter 4 I think this would be more simple because CI4 has a built in property for models that specifically allows you to manage which db a model will connect to.
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
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
);
In my site are different modules, each has its own datatable.
So now I'm not sure which is the best way to connect these modules in the best way.
My idea is to create a helper function, which tests if is a database-connection is available or not. If not, the helper should init the database and make the database for queries available in the controller and models.
In the documentation of Codeigniter I've only found information about multiple database setup - I cannot find an example for do that with a kind of dynamic helper.
Maybe someone can help me a step further?
What you can do is set session of your db_name, db_user and db_pass (if user and pass are different for each db_name). In order to reuse the dynamic db without always passing the access.
Here is an implementation:
Helper:
if (!function_exists('get_dynamic_db')){
function get_dynamic_db()
{
$CI =& get_instance();
$db = $CI->session->user_data('other_db');
$user = $CI->session->user_data('other_db_user');
$pass = $CI->session->user_data('other_db_pass');
$config_app = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => $user,
'password' => $pass,
'database' => $db,
'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
);
return $CI->load->database($config_app,TRUE);
}
}
Model:
Class DD_model extends CI_Model {
var $dynamic_db;
public function __construct() {
$this->load->database(); // default db
$this->dynamic_db = get_dynamic_db(); // dynamic db
}
public function ping_server_db()
{
$this->dynamic_db->from('some_table');
$query = $dynamic_db->get();
return $query->row() ? true : false;
}
}
Controller:
public function select_db($db_name)
{
$this->session->set_userdata(array('other_db' => $db_name, 'other_db_user' => 'user', 'other_db_pass' => 'pass'));
$dynamic_db = $this->DD_model->ping_server_db();
if (!$dynamic_db) {
$this->session->unset_userdata('other_db');
return false;
}
}
With this, you can then use get_dynamic_db(); in all your models to query from dynamic db
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.