PHP5 OO - Not sure what is going wrong [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aspnetguy
Forum Newbie
Posts: 3
Joined: Mon Apr 23, 2007 8:08 am

PHP5 OO - Not sure what is going wrong [SOLVED]

Post by aspnetguy »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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

Code: Select all

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

Code: Select all

<?php

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

?>
class/Site.php

Code: Select all

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

Code: Select all

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

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by aspnetguy on Mon Apr 23, 2007 9:14 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Not a mysql bod but at a guess you are using a trying to connect as a user called 'ODBC' with no password which is either not a user in mysql or should have a password.

As a side note you are reinventing the wheel... You may want to look at ADODB rather than recreate something new.
aspnetguy
Forum Newbie
Posts: 3
Joined: Mon Apr 23, 2007 8:08 am

Post by aspnetguy »

I'll look at ADODB but I am entering a user name and password which connect successfully when used outside this class.

And note is failing on mysql_query. It is connecting with mysql_connect, for some reason it is not recognizing the connection when I run a query.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Definitely look at your "Database" constructor and your connection information. Sounds like you've set username="ODBC" and password="". If not, then you dont have SELECT permissions (which is technically absolutely NO permissions :-p).

Also, in order to avoid any problems with your database connection(s), I suggest you make your queries look more like this:

Code: Select all

mysql_query($queryString, $this->db->GetDb());
... Where GetDB() returns $db from "Database."

And there's nothing wrong with reinventing the wheel in programming. :-p It's the best way to learn. :-D
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

superdezign wrote:And there's nothing wrong with reinventing the wheel in programming. :-p It's the best way to learn. :-D
Whilst in principle this is true, the trouble with most DB connection classes I see built from scratch is the lack of security. Depends if you need a secure project completed or are doing things for learning. Looking at something like ADODB can also give you an idea as to what you need to do if you are building your own class.

Back to the main question...

Looking at the Page.php

Code: Select all

$result = mysql_query("select * from pages where id={$this->id}");
This needs needs to be done in the database class where you have the connection built as you have no current connection in your page.php (variable scope issue).

Class Database needs to have a query method with the sql/parameters passed to it. You should do things like mysql_escape_string and possibly quote information here so users don't have to worry about coding it each time.
aspnetguy
Forum Newbie
Posts: 3
Joined: Mon Apr 23, 2007 8:08 am

Post by aspnetguy »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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

CODE

Code: Select all

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

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply