CodeIgniter错误mysql_connect(): No such file or directory解决方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeIgniter错误mysql_connect(): No such file or directory解决方法相关的知识,希望对你有一定的参考价值。
参考技术A 首先CodeIgniter连接数据库连不上,总是显示连接错误,但是又没有error信息,难以debug。解决方案是:在application/config/database.php文件的最后加上这一段代码:
复制代码
代码如下:
echo
'<pre>';
print_r($db['default']);
echo
'</pre>';
echo
'Trying
to
connect
to
database:
'
.$db['default']['database'];
$dbh=mysql_connect
(
$db['default']['hostname'],
$db['default']['username'],
$db['default']['password'])
or
die('Cannot
connect
to
the
database
because:
'
.
mysql_error());
mysql_select_db
($db['default']['database']);
echo
'<br
/>
Connected
OK:'
;
die(
'file:
'
.__FILE__
.
'-->
Line:
'
.__LINE__);
Codeigniter - 会话登录错误
【中文标题】Codeigniter - 会话登录错误【英文标题】:Codeigniter - session login bug 【发布时间】:2017-02-13 11:01:15 【问题描述】:您好,我正在编写登录和注册模块 我发现了关于这样的会话的错误。 首先我登录然后我关闭页面。之后我重新打开页面并输入路径url,这会发生......,
如果我输入路径 url "localhost/codeigniter/index.php/some-controller" ,它将显示处于登录前状态的视图页面。 但是如果我点击某些东西来更改页面,它会显示登录后状态,并且路径 url 从“localhost/codeignite/index.php/some-controller”更改为“[::1]/codeigniter/index.html”。 php/一些控制器” 所以如果我首先尝试将 [::1] 而不是 localhost 放在路径 url 中,它也会显示登录后状态。发生了什么?为什么我不能使用本地主机?如果我使用本地主机,它似乎没有会话。或者这是xampp mysql上的一个错误?<?php ///////////////view/header.php///////////
if($this->session->userdata('is_logged_in'))
echo "Welcome!, " . $this->session->userdata('username');
echo anchor('login/logout','Sign Out') ."</span>";
else
echo anchor('login','Log in');
echo anchor('register/index','Register') ;
?>
登录控制器
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
public function __construct()
parent::__construct();
$this->load->library(array('form_validation','session'));
public function index()
$this->load->view('header');
$this->load->view('regislogin/loginform');
$this->load->view('footer');
public function login_validation()
$this->load->view('header');
$this->form_validation->set_rules('username','Username','required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|md5|trim');
if($this->form_validation->run()) //==TRUE
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/userPage');
else
$this->load->view('regislogin/loginform');
public function userPage()
if($this->session->userdata('is_logged_in')) //if==true
$this->load->view('header');
$this->load->view('regislogin/success');
else
public function validate_credentials()
//$this->load->model('login_model'); //autoload already
if($this->login_model->can_login()) //if can_login == true
return true;
else
$this->form_validation->set_message('validate_credentials','Username/Password Incorrect');
return false;
public function logout()
$this->session->sess_destroy();
redirect('login');
登录模式
<?php
class Login_model extends CI_Model
public function can_login()
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('member');
if($query->num_rows() == 1) //if Found 1 match
return true;
else
return false;
注册控制器
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
public function __construct()
parent::__construct();
$this->load->library(array('form_validation','session'));
public function index()
$this->load->view('header');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[6]);
if($this->form_validation->run() == FALSE)
$this->load->view('regislogin/register_view');
else
$key = md5(uniqid());
$this->load->library('email', array('mailtype'=>'html'));//default mailtype is 'text'
$this->email->from('supanat_thana@hotmail.com', "Webmaster");
$this->email->to($this->input->post('email'));
$this->email->subject("Confirm your account");
$message = "<p>Thank you for register!</p>";
$message .= "<p><a href='".base_url()."register/register_activate/$key'>Click Here</a> เพื่อยืนยันบัญ๙ีของคุณ </p>";
$this->email->message($message);
if($this->register_model->add_temp_user($key))
if($this->email->send())
$data['emailstr'] = "Please activate your account by email";
else
$data['emailfail'] = "could not send the email.";
else
echo "Problem: cannot insert to out database";
$this->load->view('regislogin/success', $data);
public function register_activate($key)
if($this->register_model->is_key_valid($key))
if($newuser = $this->register_model->addMember($key))
$data = array(
'username' => $newuser,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/userPage');
echo "successfully activate account <br/>";
echo anchor('home', 'back to Home');
else echo "sorry cannot activate account";
else
echo "invalid key";
注册模型
<?php
class Register_model extends CI_Model
public function __construct()
parent::__construct();
public function add_temp_user($key)
$data = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'key' => $key
);
$query = $this->db->insert('temp_user', $data);
if($query) return true;
else return false;
public function is_key_valid($key)
$this->db->where('key', $key);
$query = $this->db->get('temp_user');
if($query->num_rows() == 1)
return true;
elsereturn false;
public function addMember($key)
$this->db->where('key', $key);
$temp_users = $this->db->get('temp_user');
if($temp_users)
$row = $temp_users->row();
$data = array(
'username' => $row->username,
'password' => $row->password,
'email' => $row->email,
'phone' => $row->phone
);
$did_adduser = $this->db->insert('member', $data);
if($did_adduser)
$this->db->where('key', $key);
$this->db->delete('temp_user');
return $data['username'];
elsereturn false;
public function isUserExist()
$username = $this->input->post('username');
$this->db->where('username',$username);
$query = $this->db->get('member');
if($query->num_rows() > 0)
return true;
else
return false;
public function isEmailExist()
$email = $this->input->post('email');
$this->db->where('email',$email);
$query = $this->db->get('member','temp_user');
if($query->num_rows() > 0)
return true;
else
return false;
查看/regislogin/success.php
<DOCTYPE! html>
<html>
<head>
<title> Register </title>
<meta charset="utf-8">
</head>
<body>
<?php
if($this->input->post("btn"))
echo "<h3>Thank you for your registration</h3>";
echo $emailstr."<br/><br/>";
if(isset($emailfail)) echo $emailfail."<br/><br/>"; else
echo anchor('home', 'กลับสู่หน้าหลัก');
else
echo "<h3>Successful Login</h3>";
echo "<pre>";
echo "Welcome!, ". $this->session->userdata('username');
echo "</pre>";
echo anchor('login/logout', 'Logout')."<br/>";
echo anchor('home', 'Back to Home');
?>
</body>
</html>
查看/loginregis/loginform.php
<div class="loginbox">
<?php echo form_open('login/login_validation');?>
<h2> Login </h2>
<ul id='login'>
<li>
Username: <br>
<input type='text' name='username'>
</li>
<li>
Password:<br>
<input type='password' name='password'>
</li>
<br>
<li>
<input type='submit' name='btn2' value='Log in'>
</li>
<li>
<br>
<?php echo anchor("register/index","Register");?>
</li>
</ul>
<?php echo form_close();?>
</div>
查看/regislogin/register_view.php
<head>
<meta charset="utf-8">
<title>Register</title>
</head>
<div id="wrapper_regis">
<div id="header_regis">
<h1>Register</h1>
</div>
<?php echo form_open('register');?>
<table>
<tr>
<td>Username : </td>
<td><input type="text" name="username" value=""/></td>
</tr>
<tr>
<td>Password : </td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td>Confirmed Password : </td>
<td><input type="password" name="pass_confirm" value=""/></td>
</tr>
<tr>
<td>Email : </td>
<td><input type="text" name="email" value=""/></td>
</tr>
<tr>
<td>Phone : </td>
<td><input type="text" name="phone" value=""/></td>
</tr>
</table>
<br/>
<input class="btn" type="submit" name="btn" value="สมัครสมาชิก"/>
<?php echo form_close();?>
</div>
MySQL 表
CREATE TABLE `temp_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(35) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(50) NOT NULL,
`phone` varchar(15) NOT NULL,
`key` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `member` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(50) NOT NULL,
`phone` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在配置/autoload.php
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','form');
$autoload['model'] = array('register_model','login_model');
【问题讨论】:
没有看到代码错误。这可能是由会话名称冲突引起的 【参考方案1】:请检查您的会话是否正常工作, 您是否还在控制器和注销上添加了适当的条件。 最好在钩子中添加一个条件,这样您就不必为每个控制器编写它。 也可以提供一些代码sn-p来查看更多。
【讨论】:
【参考方案2】:我导入了您的代码,它正在正常工作。 请只做下面的事情 1. 添加一个空白数组 $data = array();在索引函数中的 Register.php(controller) 内。 2. 在登录控制器里面的函数 userPage ,Else 条件添加一行重定向到登录页面
public function userPage()
if($this->session->userdata('is_logged_in')) //if==true
$this->load->view('header');
$this->load->view('regislogin/success');
else
redirect('login');
您有任何疑虑,请分享您遇到错误的页面名称。
【讨论】:
你能给我流程吗,就像你要去 index.php/login 然后 index.php/userPage 等,这样我就可以理解整个场景 1. home.php 我已经创建了,但没有得到哪个流程给你错误。只想知道你在做什么流程 7.如果把url像这样“[::1]/codeigniter/index.php/some_controller比如home,register”你会看到结果是show welcome!标题上的用户名正确。 总之,如果我在路径 url 中使用“[::1]”而不是“localhost”,会话将正常工作。是的,如果我点击其他页面,它会自动将 localhost 更改为 [::1] 并且也可以正常工作。【参考方案3】:好的,我解决了。 在文件夹路径 config/config.php 设置这个 $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/foldername';
【讨论】:
以上是关于CodeIgniter错误mysql_connect(): No such file or directory解决方法的主要内容,如果未能解决你的问题,请参考以下文章
CodeIgniter 错误、result_array 和 foreach 错误