php 类连接数据库的问题
下面这段出现这样的错误,怎么回事?Fatalerror:CannotaccessemptypropertyclassMysql{private$host;private$...
下面这段出现这样的错误,怎么回事?Fatal error: Cannot access empty property
class Mysql{
private $host;
private $name;
private $pass;
function __construct($host,$name,$pass){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->connect();
}
function connect(){
if(mysql_connect($this->$host,$this->$name,$this->$pass)){
echo "连接成功";
}else{
echo "连接失败";
}
}
}
$db=new Mysql("localhost","root",""); 展开
class Mysql{
private $host;
private $name;
private $pass;
function __construct($host,$name,$pass){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->connect();
}
function connect(){
if(mysql_connect($this->$host,$this->$name,$this->$pass)){
echo "连接成功";
}else{
echo "连接失败";
}
}
}
$db=new Mysql("localhost","root",""); 展开
2个回答
展开全部
你的connect()方法中没有设置使用参数,但是在方法中又使用了$host这些变量,当然出错咯。可以改成
<?php class Mysql{
private $host;
private $name;
private $pass;
function __construct($host,$name,$pass){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->connect($host,$name,$pass);
}
function connect($host,$name,$pass){
if(mysql_connect($host,$name,$pass)){
echo "连接成功";
}else{
echo "连接失败";
}
}
}
$db=new Mysql("localhost","root","");
?>
<?php class Mysql{
private $host;
private $name;
private $pass;
function __construct($host,$name,$pass){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->connect($host,$name,$pass);
}
function connect($host,$name,$pass){
if(mysql_connect($host,$name,$pass)){
echo "连接成功";
}else{
echo "连接失败";
}
}
}
$db=new Mysql("localhost","root","");
?>
展开全部
<?php
/*
* mysql数据库类
*
* $host 主机
* $dbbase 数据库
* $dbpassword 密码
* ------------------------------------------------
* function __construct($host,$dbbase,$dbpassword) 进行连接数据库
*
*/
class db_mysql{
private $host;
private $dbbase;
private $dbpassword;
function __construct($host,$dbbase,$dbpassword){
$this->host = $host;
$this->dbbase = $dbbase;
$this->dbpassword = $dbpassword;
$this->connnect();
}
//进行连接
function connnect(){
//打开连接
$con= mysql_connect($this->host,$this->dbbase,$this->dbpassword) or die("连接数据库失败");
}
}
$p = new db_mysql("localhost","root",""); //进行实例化
?>
/*
* mysql数据库类
*
* $host 主机
* $dbbase 数据库
* $dbpassword 密码
* ------------------------------------------------
* function __construct($host,$dbbase,$dbpassword) 进行连接数据库
*
*/
class db_mysql{
private $host;
private $dbbase;
private $dbpassword;
function __construct($host,$dbbase,$dbpassword){
$this->host = $host;
$this->dbbase = $dbbase;
$this->dbpassword = $dbpassword;
$this->connnect();
}
//进行连接
function connnect(){
//打开连接
$con= mysql_connect($this->host,$this->dbbase,$this->dbpassword) or die("连接数据库失败");
}
}
$p = new db_mysql("localhost","root",""); //进行实例化
?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询