下面有两段代码为什么执行结果不一样,帮解释,谢谢
Jun 23, 2016 pm 02:38 PM $userName="root";
$userPwd="1234";
$dbName="test";
$serverName="localhost";
//连接数据库
$conn=mysql_connect("localhost","root","1234");
//选择数据库
$select=mysql_select_db("test",$conn);
if($select){
echo"数据库连接成功";
}
?>
和下面的代码执行结果不同,请分析下 $userName="root";
$userPwd="1234";
$dbName="test";
$serverName="localhost";
//连接数据库
$this->conn=mysql_connect($this->serverName,$this->userName,$this->userPwd);
//选择数据库
$this->my_db=mysql_select_db($this->dbName,$this->conn);
if($this->my_db){
echo"数据库连接成功";
}
?>
回复讨论(解决方案)
$this->
你的在一个类中,这个类完整的代码呢?
$this->serverName
是类中的属性么?有设置么
如果你确定已贴出所有代码,建议看下PHP class。
关键是有什么不同?
class login
{
private $userName="root";
private $userPwd="1234";
private $dbName="test";
private $serverName="localhost";
function __construct()
{
//连接数据库
$this->conn=mysql_connect($this->serverName,$this->userName,$this->userPwd);
//选择数据库
$this->my_db=mysql_select_db($this->dbName,$this->conn);
mysql_query("set names gb2312"); //设置编码格式
if($this->my_db){
echo"数据库连接成功";
}
}
?>
如果连接成功,会显示“数据库连接成功”,可结果为什么不对那,说程序错误,请修改谢谢
class login
{
private $userName="root";
private $userPwd="1234";
private $dbName="test";
private $serverName="localhost";
function __construct()
{
//连接数据库
$this->conn=mysql_connect($this->serverName,$this->userName,$this->userPwd);
//选择数据库
$this->my_db=mysql_select_db($this->dbName,$this->conn);
mysql_query("set names gb2312"); //设置编码格式
if($this->my_db){
echo"数据库连接成功";
}
}
?>
如果连接成功,会显示“数据库连接成功”,可结果为什么不对那,说程序错误,请修改谢谢
报什么错?
程序按你给出的代码
类 login 的定义未结束,单独执行会报错
Parse error: syntax error, unexpected end of file
那帮我修改下,让它输出“数据库连接成功”的字样啊,谢谢啊
在 ?> 前加入
}new login;
哎哟,测试下不行啊,测试后什么都没有。你再测试下,好吧。谢谢
非常好,成功了,我刚才弄错了,谢谢啊
版主 真认真啊 很好啊

热门文章

热门文章

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)