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.phpCode: 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);
}
}
?>Code: Select all
<?php
require("class/Database.php");
require("class/Site.php");
require("class/Page.php");
$page = new Page($_GET["id"]);
echo "{$page->name}";
?>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]