简体   繁体   English

在MAMP堆栈上运行的CodeIgniter中没有数据库选择错误

[英]No database selected error in CodeIgniter running on MAMP stack

First off, does anyone know of a good place to get help with CodeIgniter? 首先,有人知道在CodeIgniter寻求帮助的好地方吗? The official community forums are somewhat disappointing in terms of getting many responses. 官方社区论坛在获得许多回应方面有些令人失望。

I have ci installed on a regular MAMP stack, and I'm working on this tutorial . 我已经在常规MAMP堆栈上安装了ci,并且正在研究本教程 However, I have only gone through the Created section, and currently I am getting a No database selected error. 但是,我只经历了Created部分,当前出现No No selected selected错误。

Model: 模型:

<?php 
class submit_model extends Model {

    function submitForm($school, $district) {
        $data = array(
            'school' => $school,
            'district' => $district
        );

        $this->db->insert('your_stats', $data);
    }

} 

View: 视图:

<?php $this->load->helper('form'); ?>

<?php echo form_open('main'); ?>

    <p>
        <?php echo form_input('school'); ?>
    </p>

    <p>
        <?php echo form_input('district'); ?>
    </p>

    <p>
        <?php echo form_submit('submit', 'Submit'); ?>
    </p>

<?php echo form_close(); ?> 

Controller: 控制器:

<?php
class Main extends controller {

    function index() {

        // Check if form is submitted
        if ($this->input->post('submit')) {
            $school = $this->input->xss_clean($this->input->post('school'));
            $district = $this->input->xss_clean($this->input->post('district'));

            $this->load->model('submit_model');
            // Add the post
            $this->submit_model->submitForm($school, $district);
        }
        $this->load->view('main_view');
    }
} 

database.php database.php

$db['default']['hostname'] = "localhost:8889";
$db['default']['username'] = "root";
$db['default']['password'] = "root";
$db['default']['database'] = "stats_test";
$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"; 

config.php config.php

$config['base_url']    = "http://localhost:8888/ci/";
...
$config['index_page'] = "index.php";
...
$config['uri_protocol']    = "AUTO"; 

So, how come it's giving me this error message? 那么,怎么会给我这个错误信息?

A Database Error Occurred
Error Number: 1046

No database selected

INSERT INTO `your_stats` (`school`, `district`) VALUES ('TJHSST', 'FairFax') 

Is there any way for me to test if CodeIgniter can actually detect the mySQL databases I've created with phpMyAdmin in my MAMP stack? 我有什么方法可以测试CodeIgniter是否可以实际检测到我在MAMP堆栈中用phpMyAdmin创建的mySQL数据库?

First check the obvious - is there a database stats_test , and can you connect using the details in your config? 首先检查显而易见的内容-是否有数据库stats_test ,您可以使用配置中的详细信息进行连接吗? You may have a problem with user permissions. 您的用户权限可能有问题。 Also check that you have a line $active_group = 'default'; 还要检查您是否有一行$active_group = 'default'; at the beginning of the database.php file, which says to use those connection parameters. 在database.php文件的开头,该文件表示要使用这些连接参数。

By the way, I'd suggest setting $db['default']['pconnect'] to false instead of true. 顺便说一句,我建议将$db['default']['pconnect']为false而不是true。 Lots of people have had connection problems using that, particularly if you have other non-CI parts of the website using a regular connection. 很多人在使用该连接时遇到连接问题,特别是如果您的网站上其他非CI部分使用常规连接时。

主机应该只是MAMP上的本地主机,或者至少是我的MAMP设置上的主机。

$db['default']['hostname'] = "localhost:8889";

mysql是否使用端口8889?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM