MySQL-我的代码中解析错误(语法错误) [英] Mysql - Parse error (syntax error) in my code

查看:64
本文介绍了MySQL-我的代码中解析错误(语法错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用时:

$mysqli = new mysqli('localhost', 'root', '', 'cms');

在我的课堂上,结果显示错误:

in my class, As a result display Error:

Parse error: syntax error, unexpected '$mysqli' (T_VARIABLE), expecting function (T_FUNCTION) in D:\xamppp\htdocs\cms\includes\td-class-db.php on line 12

当使用$mysqli = new mysqli('localhost', 'root', '', 'cms');超出类时,不会显示任何错误!

And when use $mysqli = new mysqli('localhost', 'root', '', 'cms'); outsilde the class no display any error!

我的课:

class Connect_db{
  $mysqli = new mysqli('localhost', 'root', '', 'cms');
}

为什么?

推荐答案

在类中创建成员变量时无法实例化类.您必须在构造函数或方法中执行此操作:

You can't instantiate classes when creating member variables in a class. You have to do this in the constructor or a method:

class Connect_db{
    private $mysqli;

    public function __constructor() {
        $this->mysqli = new mysqli('localhost', 'root', '', 'cms');
    }
}

这篇关于MySQL-我的代码中解析错误(语法错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆