Jump to content

PHP5 OOP - Not sure what I am doing wrong [SOLVED]


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 28Warning: 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 28Warning: 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
Share on other sites

anyone? I really want to write this app in PHP because it is platform independent. From every example I could find my code looks right but it is not recognizing the username and password.

Link to comment
Share on other sites

The weird thing is that the access denied error happens on line 28 from page.php, which, unless I'm mistaken, is this line:

$result = mysql_query("select * from pages where id={$this->id}");

It looks to me like it is not connected. This is from the reference page for mysql_query:

If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.
I'm thinking that the defaults in php.ini are probably to set the default MySQL server to be localhost, and the default user to be "ODBC". The reason I think it can't find a connection is because 1) it doesn't make sense for there to be an access denied error on a mysql_query line, if the access was denied then mysql_connect would raise the error, not mysql_query; 2) because the error message indicates that a password is not being used, and clearly you connect with a password in the database class; and 3) because the user is "ODBC", and I'm betting that is not the same username you are trying to connect with. Say if it is though.I believe that the reason why a connection is not found is because the $db property that holds the connection resource in the database class is a private property. That may cause the connection to be restricted to a single instance of that class, and the database class is being created by the page class. The page class would not have access to the private properties of the database class.I think the best solution for this is to keep the database connection encapsulated like it is, and instead to add a public query method that you can use to send a query using the connection for that class. In that case, you would probably want to modify the select_db function call to specify the connection to use, and also specify the connection to use when you make the call to mysql_query inside the query method for the db class. You can also just make the $db property public, but if you keep it private then you can have several class instances on one page and have each of them connect to a different server. But if you had several instances all connecting to the same server, they would have to use different connections. So I don't know, maybe it would be better to just make the connection variable public. Actually, it would probably be the best to make it a private static variable, that way it would still be private and multiple instances could use the same connection. In that case you might want to check if it is connected before you try to connect in the open method for the database class, you could do that using isset.
Link to comment
Share on other sites

my database class now looks like this but I still get eh same errors

<?php		class Database	{		private $server;		private $user;		private $password;		private $database;		public $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);		}					public function close()		{			mysql_close($this->db);		}	}	?>

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...