OOPS with OOP, PHP database connection
Hey guys I am developing a database connection with OOP PHP and I am stuck on this error;
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:Users...function.php on line 26.
I am new to OOP so any help greatly appreciated
<?php
class MyClass{
var $HOST = "localhost";
var $USER = "user";
var $PASS = "pass";
var $DB = "image_blog";
public $con;
function _construct(){
$this->host = $HOST;
$this->user = $USER;
$this->pass = $PASS;
$this->db = $DB;
$this->con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
}
public function query($sql)
{
$query = mysqli_query($this->con,$sql);
return $query;
}
}
$obj = new Myclass;
echo $obj->query("SELECT * FROM posts");
The name of your constructor function must be __construct
with two underscores, not one.
You can debug this sort of problem yourself. Working backwards from the error, you would first do a var_dump()
on $this->con
(the first parameter for mysqli_query()
). This would show you that $this->con
is not defined. Next, you would go back to where you define and add a die()
statement or similar to see if that code is even running. You will find that it isn't.
From there, you could try copying/pasting in a known working function from the PHP documentation and then you would find that the constructor function would work. All you have to do then is compare your two constructor functions, and it is likely you would immediately spot your error.
var $HOST = "localhost";
var $USER = "user";
var $PASS = "pass";
var $DB = "image_blog";
Reading this answer, you are using an outdated syntax for declaring class properties here (they are not, in fact, variables in and of themselves). In your constructor (which others are right, is missing the first underscore), you are simply referencing undefined local variables, rather than the properties themselves. The solution here is to:
_construct
method to make it __construct
, and I would suggest passing variables in as parameters to the constructor and set them that way (so that you would do new MyClass( $host, $user, $pass, $db)
). I would also change, instead of using the var
keyword, to declare these properties like this:
protected $host = 'host';
// etc.
Another option could be to define them as constants, since, as you are using them currently, they can never really change anyway:
const HOST = 'host';
And you can reference them later as:
self::HOST
Here's a relevant quote from the documentation:
In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning.
构造function __construct()
以2个下划线function __construct()
开头
上一篇: 在if之后期望陈述
下一篇: 带有OOP的OOPS,PHP数据库连接