<?php
class user {
public $connection;
public $host;
public $username;
public $password;
public $database;
public $port;
public function __constructor() {
$this->host = "localhost";
$this->username = "root";
$this->password = "Password";
$this->database = "database";
$this->port = "port";
// Create connection
$this->connection = new mysqli($this->host, $this->username, $this->password, $this->database, $this->port);
// Check connection
if ($this->connection->connect_error)
{
echo "Failed to connect to MySQL: " . $this->connection->connect_error;
}
}
public function save ($data) {
$prop = strval($data['prop']);
$sql = "INSERT INTO user (prop)
VALUES ('" .$prop. "')";
if (!mysqli_query($this->connection, $sql))
{
die('Error: ' . mysqli_error($this->connection));
}
echo "1 record added";
mysqli_close($this->connection);
}
}
?>
当运行代码以上得到以下警告:构造属性为null
PHP警告:mysqli_query()预计参数1是mysqli的,空 在给定的“路径:路径‘”上线32
PHP的警告mysqli_error()预计 参数1是mysqli的,在给定的空’在线路34上
任何帮助将不胜感激。
'public function __construct()'! – moonwave99
不,你不这样做,因为这段代码实际上并没有实例化任何用户。你的[testcase](http://sscce.org)在哪里? –