当前位置: 首页 > 面试题库 >

您指定了无效的数据库连接组codeigniter错误

轩辕风华
2023-03-14
问题内容

我正在从一个教程CRUD。而且我得到这个错误。

您指定了无效的数据库连接组。

有什么问题吗?

database.php- 数据库配置

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'cicrud';
$db['default']['dbdriver'] = 'mysql';
$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;

users_model.php -model

<?php

class Users_model extends CI_Model {

function __construct()

{

parent::__construct();

$this->load->database('cicrud');

}

public function get_all_users()

{

$query = $this->db->get('users');

return $query->result();

}
public function insert_users_to_db($data)

{

return $this->db->insert('users', $data);

}

}

?>

users.php- 控制器

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

function __construct()

{

parent::__construct();

#$this->load->helper('url');

$this->load->model('users_model');

}

public function index()

{

$data['user_list'] = $this->users_model->get_all_users();

$this->load->view('show_users', $data);

}
public function add_form()

{

$this->load->view('insert');

}
public function insert_new_user()

{

$udata['name'] = $this->input->post('name');

$udata['email'] = $this->input->post('email');

$udata['address'] = $this->input->post('address');

$udata['mobile'] = $this->input->post('mobile');

$res = $this->users_model->insert_users_to_db($udata);

if($res){

header('location:'.base_url()."index.php/users/".$this->index());

}

}
}

show_users.php -html视图

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>CI CRUD</title>

<script type="text/javascript">

function show_confirm(act,gotoid)

{

if(act=="edit")

var r=confirm("Do you really want to edit?");

else

var r=confirm("Do you really want to delete?");

if (r==true)

{

window.location="<?php echo base_url();?>index.php/users/"+act+"/"+gotoid;

}

}

</script>

</head>

<body>

<h2> Simple CI CRUD Application </h2>

<table width="600" border="1" cellpadding="5">

<tr>

<th scope="col">Id</th>

<th scope="col">User Name</th>

<th scope="col">Email</th>

<th scope="col">Mobile</th>

<th scope="col">Address</th>

<th scope="col" colspan="2">Action</th>

</tr>

<?php foreach ($user_list as $u_key){ ?>

<tr>

<td><?php echo $u_key->id; ?></td>

<td><?php echo $u_key->name; ?></td>

<td><?php echo $u_key->email; ?></td>

<td><?php echo $u_key->address; ?></td>

<td><?php echo $u_key->mobile; ?></td>

<td width="40" align="left" ><a href="#" onClick="show_confirm('edit',<?php echo $u_key->id;?>)">Edit</a></td>

<td width="40" align="left" ><a href="#" onClick="show_confirm('delete',<?php echo $u_key->id;?>)">Delete </a></td>

</tr>

<?php }?>

<tr>

<td colspan="7" align="right"> <a href="<?php echo base_url();?>index.php/user/add_form">Insert New User</a></td>

</tr>

</table>

</body>

</html>

问题答案:

您正在加载名为的数据库组circrud。但是,没有所谓的数据库组。您仅有的一个是称为的组default,如果您未指定组,则默认情况下将加载该组。

$this->load->database('cicrud');

你应该做

$this->load->database(); 在这部分代码中:

class Users_model extends CI_Model {

function __construct()

{

parent::__construct();

$this->load->database();

}


 类似资料:
  • PHP interbase中的Noob。 我正在做一个项目,我需要将数据保存在两个独立的数据库中。我使用默认的MySQL数据库,而另一个使用firebird。已下载此库 这是我的数据库。配置文件夹中的php。 需要多个数据库的函数 模型 我对create_purchaseorder函数没有问题,但是当我运行“save”函数时,它会给我这个错误 致命错误:调用未定义的函数ibase_connect(

  • 问题内容: 我正在运行一个使用c3p0作为连接池的Spring / Hibernate连接到MySQL安装程序。出于某些奇怪的原因,当系统处于负载状态时(当然),它用尽了连接。 该网站相当稳定,直到我们开始达到新的流量水平(并发用户超过100个)。届时,DB将会崩溃(与CPU挂钩)。我的第一个动作是在应用程序中通过广泛的缓存和查询优化等来提高性能。 现在,它会间歇性地耗尽连接。似乎甚至都不依赖于负

  • 问题内容: 我正在尝试连接到本地oracle数据库,但是却收到此错误消息: 。 我很确定这是由于我传递的数据库连接参数出错,但实际上,此错误消息对我没有任何帮助。关于我做错了什么的任何提示将不胜感激。 仅供参考:用于连接的代码如下,除了硬编码的字符串,这是在我们的生产环境中使用并在此处工作的代码。 问题答案: 令人惊讶的是,在将以下两行添加到创建连接的代码中之后,它开始工作了。 我不明白为什么我们

  • 问题内容: 我一直在使用CI驱动程序和MySQL驱动程序。我想改用MySQL驱动程序,但是一旦更改它(只需在MySQL末尾添加“ i”并添加端口号),我就会收到以下错误消息 发生数据库错误 无法使用提供的设置连接到数据库服务器。 文件名:core / Loader.php 行号:232 我的设置如下所示: 我在用着: CI 2.0.2 php 5.3.4 Apache / 2.2.17(Unix)

  • 从下面描述的tsk开始,我在一个看似简单的过程中遇到了无数问题。但首先是一些背景: Windows 10 QGIS 2.18.5 PgAdmin 4(v 2.0) Postgreql 10安装 -Postgreql数据库(托管在运行PostgreSQL 9.6.5的Amazon AWS云上) 我对postgreql和postgis非常陌生,但是在遵循所有基本说明之后,我似乎无法在QGIS中查看任何

  • 我使用Laravel 5和我的数据库连接有问题: 这是我的database.php文件: 这是我的. env文件: DB_HOST=localhost DB_数据库=我的_场景 DB_USERNAME=root DB_PASSWORD=null 然而,当我运行“php artisan migrate”时,我得到了一个错误 [PDOException]SQLSTATE[HY000][1049]未知数