• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

下面有两段代码为什么执行结果不一样,帮解释,谢谢

php 搞代码 4年前 (2022-01-23) 16次浏览 已收录 0个评论
文章目录[隐藏]

$userName=”root”;
$userPwd=”1234″;
$dbName=”test”;
$serverName=”localhost”;
//连接数据库
$conn=mysql_connect(“localhost”,”root”,”1234″);
//选择数据库
$select=mysql_select_db(“test”,$conn);
if($select){
echo”数据库连接成功”;
}
?>
和下面的代码执行结果不同,请分析下 <?php
$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。

关键是有什么不同?

<?php
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”数据库连接成功”;
}

}
?>
如果连接成功,会显示“数据库连接成功”,可结果为什么不对那,说程序错误,请修改谢谢

<?php
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”数据库连接成功”;
}

}
?>
如果连接成功,会显示“数据库连接成功”,可结果为什么不对那,说程序错误,请修改谢谢
报什么错?

程序按你给出

!本文来源gaodai.ma#com搞#代!码(网

搞gaodaima代码的代码
类 login 的定义未结束,单独执行会报错
Parse error: syntax error, unexpected end of file

那帮我修改下,让它输出“数据库连接成功”的字样啊,谢谢啊

在 ?> 前加入

}new login;

哎哟,测试下不行啊,测试后什么都没有。你再测试下,好吧。谢谢

非常好,成功了,我刚才弄错了,谢谢啊

版主 真认真啊 很好啊


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:下面有两段代码为什么执行结果不一样,帮解释,谢谢

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址