【问题标题】:codeigniter helloworld app not running for some reasoncodeigniter helloworld 应用程序由于某种原因没有运行
【发布时间】:2016-05-06 15:57:41
【问题描述】:

我正在尝试学习 here 教程,这只是一个从数据库中提取数据的简单 helloworld。

我正在使用 CI 3.0.6。

我以前从未使用过codeigniter,但我想尝试一个框架。

无论如何,我认为我正确地遵循了本教程(我确实认为本教程使用了稍旧版本的 codeigniter,因为某些路径有点不同)但我无法让它工作,它给了我错误 @987654323 @

我对此有几个问题。

教程告诉我要使用模型,但什么是模型

我怎样才能让它工作?如果我能让它正常工作,我毫不怀疑我可以将它作为一种学习经验来扩展我的项目。

我的代码: helloworld.php(控制器@BASEDIR/application/controllers)_:

<?php
    class Helloworld extends Controller{
        function index()
        {
            $this->load->model('helloworld_model');

            $data['result'] = $this->helloworld_model-><span class="sql">getData</span>();
            $data['page_title'] = "CI Hello World App!";

            $this->load->view('helloworld_view',$data);
        }
    }
?>

helloworld_model.php(模型@BASEDIR/application/models):

<?php
class Helloworld_model extends Model {

    function Helloworld_model()
    {
        // Call the Model constructor
        parent::Model();
    }

    function getData()
        {
            //Query the data table for every record and row
            $query = $this->db->get('data');

            if ($query->num_rows() > 0)
            {
                //show_error('Database is empty!');
            }else{
                return $query->result();
            }
        }

}
?>

helloworld_view(查看@BASEDIR/application/views):

<html>
    <head>
        <title><?=$page_title?></title>
    </head>
    <body>
        <?php foreach($result as $row):?>
        <h3><?=$row->title?></h3>
        <p><?=$row->text?></p>
        <br />
        <?php endforeach;?>
    </body>
</html>

我的数据库信息:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'private',
    'username' => 'private',
    'password' => 'private',
    'database' => 'database',
    'dbdriver' => 'mysql';
    'dbprefix' => 'va',
    '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
);

&我的自动加载:

$autoload['libraries'] = array('database');

我正在尝试通过 http://myurl.com/dev/index.php/helloworld/ 访问该页面

/dev/ 是我的安装目录。

同样,我以前从未使用过框架,所以这个问题可能很容易解决。

【问题讨论】:

  • 您使用的是哪个 ci 版本?
  • 您的数据库凭据也错误
  • @SoravGarg 3.0.6 - 你是什么意思他们错了?
  • 在主机名中您插入的唯一冒号
  • @SoravGarg 不,它在我的代码中是正确的信息,我刚刚在 Stack 上清除了它。

标签: php codeigniter


【解决方案1】:

如果您完全按照该教程进行操作,那么您需要在 url 中没有 index.php 的情况下访问该页面。

也使用 PHP5 构造函数,所以改变:

function Helloworld_model()
{
    // Call the Model constructor
    parent::Model();
}

收件人:

function __construct()
{
    // Call the Model constructor
    parent::__construct();
}

这也没有意义:

if ($query->num_rows() > 0)
{
    //show_error('Database is empty!');
}
else
{
    return $query->result();
}

if/else 里面的代码应该切换。如果返回的行数大于0,则返回结果,否则显示数据库为空的错误。

控制器中的最后一件事:

$data['result'] = $this->helloworld_model-><span class="sql">getData</span>();

仅将数据库结果存储到数据变量中,将其传递给视图并在视图中包装数据的任何元素。

$data['result'] = $this->helloworld_model->getData();

【讨论】:

    【解决方案2】:

    如果您阅读文档控制器 enter link description here

    你可以看到在 application/controller/Name.php 中控制器的名字需要大写,在你需要的类里面(记住扩展 CI_Controller)

    <?php
    class Controller_name extends CI_Controller {
    
            public function view($page = 'home')
            {
            }
    }
    

    对模型的调用是好的$this-&gt;load-&gt;model('helloworld_model');m,但请记住,在 applicatoin/models/Model_name.php 中也需要大写,并且你需要有(记住扩展 CI_Model)

    <?php 
    class Blog_model extends CI_Model {
    
            public function __construct()
            {
                    // Call the CI_Model constructor
                    parent::__construct();
            }
    

    是的,您需要自动加载数据库,但是在您的数据库配置(application/config/database.php)中将 dbdriver 更改为 mysqli

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多