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 ?

4
  • Don't use mysql as it is depreicated. Use mysqli instead Commented Nov 28, 2014 at 0:56
  • Why on earth are you turning off errors?! We cannot help you if you do not know what the problem is. Commented Nov 28, 2014 at 0:56
  • Please, don't use mysql_* functions, They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. This article will help you decide. Commented Nov 28, 2014 at 0:59
  • Look at how you made an easy task seem hard? This code is too big just to connect to a MySQL DB! Commented Nov 28, 2014 at 0:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.