php 中的全局变量弄乱了我的 html!
我有一个问题。当我在 php 代码中使用全局变量时,我的服务器根本不显示任何 html 代码。如果我注释掉全局变量,我的 html 页面就可以正常工作了!我在这里做错了什么吗?
php 文件:
class DBConnect{
// If I comment this out, the HTML shows
global $con;
function connectDB() {
$user = "bstokni_basurin";
$pass = "basurin";
$database = "basurin";
$host = "localhost";
$this->$con = mysql_connect($host, $user, $pass) or die(mysql_error());
echo "Connected to MySQL </br>";
echo $con;
}
function closeDB() {
mysql_close($con);
echo $con;
echo "MySQL closed";
}
}
html 文件:
<!-- Left colon -->
<div id="leftCol">
<p>Her kemur ein menu at standa</p>
<?
$menuObj = new DBConnect();
$menuObj->connectDB();
?>
</div>
我在这里做错了什么?
I have a problem. When I use global variables in my php code, my server doesn't show any html code at all. If I comment out the global variable, my html page works just fine! Am I doing something wrong here?
php file:
class DBConnect{
// If I comment this out, the HTML shows
global $con;
function connectDB() {
$user = "bstokni_basurin";
$pass = "basurin";
$database = "basurin";
$host = "localhost";
$this->$con = mysql_connect($host, $user, $pass) or die(mysql_error());
echo "Connected to MySQL </br>";
echo $con;
}
function closeDB() {
mysql_close($con);
echo $con;
echo "MySQL closed";
}
}
html file:
<!-- Left colon -->
<div id="leftCol">
<p>Her kemur ein menu at standa</p>
<?
$menuObj = new DBConnect();
$menuObj->connectDB();
?>
</div>
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于该变量位于类范围内,因此请尝试将
global
更改为public
。在您提供的示例中,您似乎不需要全局。Since the variable is in a class scope, try changing
global
topublic
instead. You don't seem to need global in the example you have provided.不应该:
是:
?
如果您只是尝试访问名为
con
的成员变量。您引用的其他地方都只是$con
,为什么$this
放在一个地方?我不确定$this->$con
应该做什么,但我猜测它会受到$con
是否已声明为全局的影响。Shouldn't:
be:
?
If you're just trying to access a member variable named
con
. Everywhere else you refer to is by just$con
, why the$this
in the one place? I'm not sure what$this->$con
is supposed to do, but I'm guessing it is affected by whether or not$con
has been declared global.