bomiha 发表于 2014-7-14 11:52:54

application\controllers\news.php on line 20语法错误

新手求教:按手册写“读取新闻条目”代码,出现语法错误Parse error: syntax error, unexpected T_PUBLIC in C:\mycx\Apache2\Ci\application\controllers\news.php on line 20,这一行代码是:
public function index()
{
检查,不知道错误在哪,请高手赐教!

bomiha 发表于 2014-7-14 11:55:19

按手册写的代码如下:    public function index()
{
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';
       
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
}

bomiha 发表于 2014-7-14 11:55:09

按手册写的代码如下:    public function index()
{
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';
       
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
}

bomiha 发表于 2014-7-17 14:19:42

创建新闻条目时,输入http://localhost/index.php/news/后显示
A PHP Error was encountered
Severity: Notice

Message: Undefined property: News:db

Filename: core/Model.php

Line Number: 51


Fatal error: Call to a member function get() on a non-object in C:\mycx\Apache2\CodeIg\application\models\news_model.php on line 26

hosinoruri 发表于 2014-7-14 14:01:07

看看第19行是否漏了}或者;,并且检查一下class X extends CI_controller里面的代码是否正确。

angusouyang 发表于 2014-7-15 14:53:38

本帖最后由 angusouyang 于 2014-7-15 15:10 编辑

应该是因为你这个方法没有放在 News 类里面

sn01615 发表于 2014-7-16 14:58:41

本帖最后由 sn01615 于 2017-4-10 02:48 编辑

语法错误 错误应该在这一行的上面

bomiha 发表于 2014-7-16 21:49:42

谢谢回答,但我从头做过了,有时又不是这个错误,现在出现在这样的的显示:
A PHP Error was encountered
Severity: Warning

Message: preg_match() : Compilation failed: missing ) at offset 5

Filename: core/Router.php

Line Number: 381

CodeIgniter 2 Tutorial
鍕ゅ伐鍔╁娆㈣繋浣�©2014

mtzai 发表于 2014-7-17 09:01:37

是不是没有引入news_mode模型。

bomiha 发表于 2014-7-17 14:17:14

重新做了,现在又能这样的错误A PHP Error was encountered
Severity: Notice

Message: Undefined property: News::$db

Filename: core/Model.php

Line Number: 51


Fatal error: Call to a member function get() on a non-object in C:\mycx\Apache2\CodeIg\application\models\news_model.php on line 26
页: [1] 2
查看完整版本: application\controllers\news.php on line 20语法错误