2012-02-06 84 views
0

我的日曆腳本工作得很好,直到將其包含到我的模板中。 events.php中的html格式正確,但與數據庫的連接沒有處理,導致下面顯示的錯誤。爲什麼會發生這種情況,連接丟失/未處理?Mysql:在包含()後,連接到數據庫會丟失()

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

錯誤:

警告:mysql_connect()函數[function.mysql-連接]:拒絕訪問用戶 '沒人' @ 'localhost' 的(使用密碼:NO)在/ home /社交上線/public_html/files/cal/smoothcalendar.php 19

警告:mysql_select_db()預計參數2是資源,在布爾給出/home/social/public_html/files/cal/smoothcalendar.php第21行

警告:mysql_query()預計參數2是資源,布爾在195行上提供/home/social/public_html/files/cal/smoothcalendar.php

無法運行查詢: 警告:mysql_close()期望參數1爲資源,布爾值在/home/social/public_html/files/cal/smoothcalendar.php給出線38

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); 
+2

我建議不要使用$ GLOBALS – 2012-02-06 06:42:29

+1

我建議使用PDO。 – Shoe 2012-02-06 06:44:07

+0

@ J.Bruni我將$ GLOBALS變量切換到$ conn,仍然是相同的錯誤 – acctman 2012-02-06 06:58:02

回答

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);