Mysql:Include()后连接到db丢失

时间:2012-02-06 06:39:30

标签: php mysql

我的日历脚本完全正常,直到我将其包含在我的模板中。 events.php中的html格式正常,但未处理与数据库的连接,导致您在下面看到的错误。为什么会发生这种情况,连接丢失/未处理?

<?php include ROOT . '/files/cal/events.php';?>

错误:

警告:mysql_connect()[function.mysql-connect]:/home/social/public_html/files/cal/smoothcalendar.php上的用户'nobody'@'localhost'(使用密码:NO)拒绝访问19

警告:mysql_select_db()期望参数2为资源,布尔值在第21行的/home/social/public_html/files/cal/smoothcalendar.php中给出

警告:mysql_query()要求参数2为资源,布尔值在第195行的/home/social/public_html/files/cal/smoothcalendar.php中给出

无法运行查询: 警告:mysql_close()要求参数1为资源,布尔值在第38行的/home/social/public_html/files/cal/smoothcalendar.php中给出

smoothcalendar.php

<?php
$server   = "localhost";
$username = "****"; 
$password = "****";
$dbname   = "***_socialdb";

class SmoothCalendar {

    private $options = null,
            $get,
            $post;

    private $connection;

    function __construct($options) 
    {
        $this->connection = mysql_connect($GLOBALS["server"  ],
                                          $GLOBALS["username"],
                                          $GLOBALS["password"]);

        mysql_select_db($GLOBALS["dbname"],$this->connection);

1 个答案:

答案 0 :(得分:2)

您可能在函数或其他内容中包含include,即全局上下文中的。这就是使用$ GLOBALS数组无法达到你的变量的原因......它们不是全局的。

这应该有效:

<?php

class SmoothCalendar {

    private $options = null,
            $get,
            $post;

    private $connection;

    private $server   = "localhost";
    private $username = "****"; 
    private $password = "****";
    private $dbname   = "***_socialdb";

    function __construct($options) 
    {
        $this->connection = mysql_connect($this->server,
                                          $this->username,
                                          $this->password);

        mysql_select_db($this->dbname, $this->connection);