MySQL Forums
Forum List  »  PHP

Re: Unknown MySQL server host 'DB_SERVER' (11004)
Posted by: Rosalie Stratford
Date: June 12, 2009 11:42AM

Thanks:

This has been copied as per instructed by the lynda.com tutorial:

"index.php"

<?php
require_once("../includes/database.php");

if(isset($database)) {
echo "true";
} else {
echo "false";
}
echo "<br />";

echo "Is this working?";


?>

This is the class page - database.php:

<?php
require_once("config.php");

class MySQLDatabase {

private $connection;

function __construct() {
$this->open_connection();
}

public function open_connection() {
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
} else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}

public function close_connection() {
if(isset($this->connection)) {
mysql_close($this->connection);
unset($this->connection);
}
}

public function query($sql) {
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}

public function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string");// i.e. PHP >= v4.3.0
if($new_enough_php) { //PHP v4.3.0 or higher
//undo any magic quote effects so mysql_real_escape_string can do the work
if($magic_quotes_active) {
$value = stripslashes($value);
}
$value = mysql_real_escape_string($value);
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if(!$magic_quotes_active) {
$value = addslashes($value);
}
// if magic quotes are active, then the slashes already exist
}
return $value;
}

private function confirm_query($result) {
if (!$result) {
die("Database query failed: " . mysql_error());
}
}
}

$database = new MySQLDatabase();
/*$db =& $database //not necessary but if want to use $db, it will reference.*/

?>

This is the defined constants page (config.php):

<?php

//Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "gallery");
defined('DB_PASS') ? null : define("DB_PASS", "phpOTL123");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>

Options: ReplyQuote


Subject
Written By
Posted
Re: Unknown MySQL server host 'DB_SERVER' (11004)
June 12, 2009 11:42AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.