Warning: mysqli_query() expects parameter 1 to be mysqli, object given in
亲们,麻烦看清楚是mysqli不是mysql<?phpclassmysqlis{private$host;private$name;private$pass;privat...
亲们,麻烦看清楚是mysqli 不是mysql
<?php
class mysqlis{
private $host;
private $name;
private $pass;
private $sqlname;
function __construct($host,$name,$password,$sqlname){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->sqlname=$sqlname;
$this->connect();
}
function connect(){
mysqli_connect($this->host,$this->name,$this->pass,$this->sqlname) or die("数据库连接错误");
}
}
$mysql=new mysqlis("localhost","dafddf","dfdfgfg","test");
$sql="select * from archives where title like '%说说%'";
$query=mysqli_query($mysql, $sql);
?>
为什么会报错? 展开
<?php
class mysqlis{
private $host;
private $name;
private $pass;
private $sqlname;
function __construct($host,$name,$password,$sqlname){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->sqlname=$sqlname;
$this->connect();
}
function connect(){
mysqli_connect($this->host,$this->name,$this->pass,$this->sqlname) or die("数据库连接错误");
}
}
$mysql=new mysqlis("localhost","dafddf","dfdfgfg","test");
$sql="select * from archives where title like '%说说%'";
$query=mysqli_query($mysql, $sql);
?>
为什么会报错? 展开
展开全部
即然你是用类封装mysql操作,那应该把所有操作都放到类中!
如果实在要这么做, 那给你改一下试试行不行
class mysqlis{
private $host;
private $name;
private $pass;
private $sqlname;
public $Conn;
function __construct($host,$name,$password,$sqlname){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->sqlname=$sqlname;
$this->connect();
}
function connect(){
$this -> Conn = mysqli_connect($this->host,$this->name,$this->pass,$this->sqlname) or die("数据库连接错误");
}
}
$mysql=new mysqlis("localhost","dafddf","dfdfgfg","test");
$sql="select * from archives where title like '%说说%'";
$query=mysqli_query($mysql -> Conn, $sql);
?>
你这出现错误, 是因为, msyqli_query()函数的第一个参数, 必须是msyqli_connect()函数操作成功后返回的数据库连接标识, 很明显, 你这个$msyql只是实例化后的class mysqlis这个类本身, 并不是数据库连接返回的标识, 那当然无法query查询!
mysqli比不msyql函数, mysql函数是可以省略数据库连接标识的, 默认为上一个连接, 而mysqli函数不行, 必须提供这个参数!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |