Jump to content

[HELP] Unknown MySQL server host 'DB_SERVER' (11004)


Recommended Posts

hi guys ,, i am studying OOP in php until i encounter this error :

 

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'DB_SERVER' (11004) in C:\xampp\htdocs\photo_exercises\includes\database.php on line 12

 

Fatal error: Cannot access empty property in C:\xampp\htdocs\photo_exercises\includes\database.php on line 13

 

i use the following code:

 

*database.php

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


   class MYSQLDatabase{
	    private $connection;
	    function __construct() {
	    $this->open_connection();
	    }

	    public function open_connection(){
			    $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($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();
   $database->close_connection();

   ?>

 

*config.php

 <?php
   //if is not define then do nothing otherwise take a second and define it
   // Database Constants
   defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
   defined('DB_USER')   ? null : define("DB_USER", "root");
   defined('DB_PASS')   ? null : define("DB_PASS", "");
   defined('DB_NAME')   ? null : define("DB_NAME", "photos");

 

*index.php

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

if(isset($database)){
    echo "true";
}else{
echo "false";
}
echo "<br/>";
echo $database->mysql_prep("it's working? <br/>");
?>

What you've shown aside, DB_SERVER is not defined. That must mean the config.php you've posted was not included. Is it in the same folder as your index.php? That's where PHP is looking. Whatever file is there is the one that was included.

 

The other error is because the syntax for getting to class variables is

$this->connection

Note how there's only one $ in there - you don't put it on the "connection" as well. You've done that a few times (and inconsistently).

Edited by requinix

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'DB_SERVER' (11004) inC:\xampp\htdocs\photo_exercises\includes\database.php on line 12

Database connection failed: Unknown MySQL server host 'DB_SERVER' (11004)

 

it gives me this error now. no, they are not in the same folder, pls help me on how to solve this.

Edited by PFMaBiSmAd
removed formatting

Please don't mess about with the fonts, it makes posts annoying to read.

 

Also, DB_SERVER is parsed as a string, which means the constant is never defined. You have to find out why. Most likely, as requinix stated above, your config file isn't included properly. Could also be a typo, or something else.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.