Jump to content

PHP5 OO - Not sure what is going wrong


aspnetguy

Recommended Posts

I am trying to setup up a daatabase object in PHP5 so I don't have to write the mysql_connect and mysql_select_db statements all the time along with the error checking and messages. Here is my object and the 3 related pages and the errors I get. The username and password is correct because I can manually write out the connect and select statements and successfully connect to the database.

 

UPDATE: I updated the code and error messages, I worked through a coupe of the errors but can't figure out why it is not connecting. It is acting like it is not recognizing the username and password.

 

class/Database.php

<?php
    
    class Database
    {
        private $server;
        private $user;
        private $password;
        private $database;
        private $db;
        
        public function __construct()
        {
            $this->server = "localhost";
            $this->user = "xxxxx";
            $this->password = "xxxxx";
            $this->database = "xxxxx";
        }
        
        public function open()
        {
            $this->db = mysql_connect($this->server,$this->user,$this->password);
            if(!$this->db) die("Unable to connect to database server!");
            
            mysql_select_db($this->database) or die("Unable to select database!");
        }    
        
        public function close()
        {
            mysql_close($this->db);
        }
    }
    
?>

 

index.php

<?php

     require("class/Database.php");
     require("class/Site.php");
     require("class/Page.php");
    
     $page = new Page($_GET["id"]);
     echo "{$page->name}";

?>

 

class/Site.php

<?php

    class Site
    {
        private $db;
        public $template;
        public $description;
        public $keywords;
        public $name;
        public $homepage;
        public $notes;
        
        public function __construct()
        {
            $this->init();
        }
        
        private function init()
        {
            $this->db = new Database();
            $this->db->open();
            $result = mysql_query("select * from site");
            while($row = mysql_fetch_array($result))
            {    
                $this->template = $row["default_template"];
                $this->description = $row["description"];
                $this->keywords = $row["keywords"];
                $this->name = $row["name"];
                $this->homepage = $row["homepage"];
                $this->notes = $row["notes"];
            }
            $this->db->close();
            
            if(strlen($this->homepage) < 1)
                die("Homepage is not set!");
        }
    }

?>

 

 

class/Page.php

<?php

    class Page
    {
        private $id;
        private $db;
        public $name;
        public $description;
        public $keywords;
        public $notes;
        public $secure;
        public $template;
        
        public function __construct($id)
        {
            $this->id = $id;
            $this->init();
        }
        
        private function init()
        {
            $this->db = new Database();
            $this->db->open();
            $site = new Site();
            if(strlen($this->id) < 1)
                $this->id = $site->homepage;
                
            $result = mysql_query("select * from pages where id={$this->id}");
            while($row = mysql_fetch_array($result))
            {
                $this->name = $row["name"];    
                $this->description = (strlen($row["description"]) < 1) ? $site->description : $row["description"];
                $this->keywords = (strlen($row["keywords"]) < 1) ? $site->keywords : $row["keywords"];
                $this->notes = $row["notes"];
                $this->secure = ($row["secure"] == "1") ? true : false;
                $this->template = ($row["template"] == "default") ? $site->template : $row["template"];
            }
            $this->db->close();
        }
    }

?>

 

 

errors

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Inetpub\wwwroot\dev\xengine\class\Page.php on line 28

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Inetpub\wwwroot\dev\xengine\class\Page.php on line 28

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\dev\xengine\class\Page.php on line 29

Link to comment
https://forums.phpfreaks.com/topic/48275-php5-oo-not-sure-what-is-going-wrong/
Share on other sites

ok I got it working. I had to specify which connection to use with mysql_query

 

<?php
    
    class Database
    {
        private $server;
        private $user;
        private $password;
        private $database;
        private $db;
        
        public function __construct()
        {
            $this->server = "localhost";
            $this->user = "root";
            $this->password = "sonicflood";
            $this->database = "cms";
        }
        
        public function open()
        {
            $this->db = mysql_connect($this->server,$this->user,$this->password);
            if(!$this->db) die("Unable to connect to database server!");

            mysql_select_db($this->database) or die("Unable to select database!");
        }
        
        public function query($query)
        {
            return mysql_query($query,$this->db);
        }    
        
        public function close()
        {
            mysql_close($this->db);
        }
    }
    
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.