0
votes

my calendar script works perfectly fine until I include it into my template. html within events.php is formatted fine but the connection to the database is not processed resulting in the errors you see below. Why is this happening, connection being lost/not processed?

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

Errors:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/social/public_html/files/cal/smoothcalendar.php on line 19

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/social/public_html/files/cal/smoothcalendar.php on line 21

Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/social/public_html/files/cal/smoothcalendar.php on line 195

Could not run query: Warning: mysql_close() expects parameter 1 to be resource, boolean given in /home/social/public_html/files/cal/smoothcalendar.php on line 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);
1
I recommend not using $GLOBALS - J. Bruni
@J.Bruni i switched the $GLOBALS variable to $conn and still same error - acctman
@acctan: Have you defined it in the same scope you are using the variable? Please read the introduction of this manual page: php.net/manual/en/language.variables.scope.php - J. Bruni
volunteer editors, PLEASE do not format error messages as the code, making them harder to read. thanks - Your Common Sense

1 Answers

2
votes

You probably have your include inside a function or something else, i.e., not in the global context. That's why your variables can't be reached using the $GLOBALS array... they're not global.

This should work:

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