php类中访问自己类内的属性或者方法都需要加 $this->
class UserImpl{
private $conn;
private $sql;
private $result;
function connect(){
$servername = "localhost";
$username = "root";
$password = "12345";
$dbname = "db";
// 创建连接
$con = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($con->connect_error) {
die("连接失败: " . $con->connect_error);
}
$this->$conn = $con;
}
function insert($name, $password){
$sql = "INSERT INTO user (name, password)
VALUES ('$name', '$password')";
if ($this->$conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $this->$conn->error;
}
$this->$conn->close();
}
function select($str){
$sql = "select * from user where".$str;
$result = $conn->query($sql);
if($result->num_rows > 0){
return $result;
}else{
return 0;
}
}
function delete($str){
$sql = "delete from user where".$str;
if($conn->query($sql) == TRUE){
echo "SUCCESS";
}else{
echo "delete".$conn->error;
}
}
}