Diether Posted January 22, 2013 Share Posted January 22, 2013 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/>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/273476-help-unknown-mysql-server-host-db_server-11004/ Share on other sites More sharing options...
requinix Posted January 22, 2013 Share Posted January 22, 2013 (edited) 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 January 22, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/273476-help-unknown-mysql-server-host-db_server-11004/#findComment-1407429 Share on other sites More sharing options...
Diether Posted January 22, 2013 Author Share Posted January 22, 2013 (edited) 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 January 22, 2013 by PFMaBiSmAd removed formatting Quote Link to comment https://forums.phpfreaks.com/topic/273476-help-unknown-mysql-server-host-db_server-11004/#findComment-1407442 Share on other sites More sharing options...
Christian F. Posted January 22, 2013 Share Posted January 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273476-help-unknown-mysql-server-host-db_server-11004/#findComment-1407456 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.