2

i've tried everything to make my site connect to the database but i always get his error: Could not connect to Master Database i have 2 files

define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','root');
define('DBNAME','test');
define('dbslave','test');  
define('dbsiteid','1');
define('dbprefix','_blog');

and connect.php

error_reporting(0);
$connect = mysql_connect("$DBHOST", "$DBUSER", "$DBPASS");
if ( ! $connect) {
    die('Could not connect to Database server');
}
$siteid  = "$dbsiteid";
$prefix  = "$dbprefix";
$dbmast  = "$DBNAME";
$dbslave = "$dbslave"; 
$cmast   = mysql_select_db("$DBNAME");
if ( ! $cmast) {
    die('Could not connect to Master Database');
}
$cslave = mysql_select_db("$dbslave");
if ( ! $cslave) {
    die('Could not connect to Slave Database');
}

how do i solve this error with connection or what i did wrong ?

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
Iskono Morto
  • 79
  • 1
  • 7
  • Don't use mysql as it is depreicated. Use mysqli instead – dannmate Nov 28 '14 at 00:56
  • Why on earth are you turning off errors?! We cannot help you if you do not know what the problem is. – Sverri M. Olsen Nov 28 '14 at 00:56
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – sjagr Nov 28 '14 at 00:59
  • Look at how you made an easy task seem hard? This code is too big just to connect to a MySQL DB! – DLastCodeBender Nov 28 '14 at 00:59

1 Answers1

7

You do not put constants in quotes, they do not start with $, and by convention are all uppercase.

 define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','root');
define('DBNAME','test');
define('DBSLAVE','test');  
define('DBSITEID','1');
define('DBPREFIX','_blog');

$connect = mysql_connect(DBHOST, DBUSER, DBPASS);
if (!$connect) {die('Could not connect to Database server');}
$cmast = mysql_select_db(DBNAME);
if (!$cmast) {die('Could not connect to Master Database');}
$cslave = mysql_select_db(DBSLAVE);
if (!$cslave) {die('Could not connect to Slave Database');}

Also, defining constants only to assign them to variables is silly and a waste of resources. And don't turn off error reporting when developing as it hides your errors. You want to do the opposite and them them on.

John Conde
  • 217,595
  • 99
  • 455
  • 496