发布网友 发布时间:2022-04-07 18:01
共2个回答
热心网友 时间:2022-04-07 19:30
/*把这个方法的参数写错了*/
热心网友 时间:2022-04-07 20:48
你到底是msyql还是mysqli?追答<?php
class Mysql{
private $host, $user, $pwd, $dbName, $charset;
private $conn = object;
public function __construct($host = 'localhost', $user = 'root', $pwd = '', $dbName = 'test', $charset = 'utf8'){
$this -> host = $host;
$this -> user = $user;
$this -> pwd = $pwd;
$this -> dbName = $dbName;
$this -> charset = $charset;
$this -> connect();
}
private function connect(){
$this -> conn = @new mysqli( $this->host, $this->user, $this->pwd, $this->dbName);
!mysqli_connect_errno() or die('数据库连接失败(' . mysqli_connect_errno() . '):' . mysqli_connect_error() );
$this->conn->query('set names ' . $this->charset);
}
public function query($sql){
return $this->conn->query( $sql );
}
}
$mysql = new Mysql;
$sql = "insert into user values (6, 'mc', 99)";
echo $mysql->query( $sql ) ? 'query ok' : 'fail';
你没有理解到类的结构好作用!
既然使用类, 那最基本的是有类成员和类方法!
类成员和方法之间可以相互调用