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 ?
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.
Related
I have a script and when I go to the website link(http://kodle.co.uk/login.php) the script comes back with this:
Warning: mysql_connect(): Access denied for user 'root'#'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 12
Warning: mysql_select_db(): Access denied for user 'hkode'#'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 13
Warning: mysql_select_db(): A link to the server could not be established in >/home/hkode/public_html/dbconnect.php on line 13
Connection failed : Access denied for user 'hkode'#'localhost' (using password: >NO)
The DBCONNECT.php script is as follows:
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'login_base');
$conn = mysql_connect("localhost", "root", "");
$dbcon = mysql_select_db(DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysql_error());
}
Go to your database and check if user root actually does not require a password. If it does then insert it as a third parameter in the empty single quotes below. Now use the code below and you will be just fine. Happy new year.
<?php
$con = mysql_connect('REPLACE_THIS_WITH_IP_ADDRESS_OF_YOUR_SERVER', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("login_base", $con);
?>
define('DBPASS', '');
You want to replace '' with 'youractualpassword'
Then change
mysql_connect("localhost", "root", "");
to
mysql_connect(DBHOST, DBUSER, DBPASS);
On a further note the comment
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
Suggest you don't use these methods. You should only do this if you absolutely know what you are doing, which seems like you do not.
I highly suggest using something like laracasts to learn php.
Good luck
It seems like the password is not right for user root also i would suggest you to use the constants for connecting to mysql which u have already defined.
i would rewrite connection statement and subsequent statement something like this
$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}else{ // connection succesful
$dbcon = mysql_select_db(DBNAME);
}
Try this
$conn = mysql_connect(DBHOST, DBUSER);
$dbcon = mysql_select_db(DBNAME, $conn);
Edited Check all passwords
mysql is deprecated you have to use mysqli or pdo. example of mysqli is ...
$hostname= "localhost";
$username = "root";
$password = "";
$dbname = "your_database_name";
$con = new mysqli($hostname, $username, $password,$dbname);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "your_query";
if ($con->query($sql) === TRUE) {
echo "On query result is successfull";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
I'm connecting to Mysql Database With following code. I'm using mysqli. But It's not connecting to mysql database.
define("HOST", "hostname");
define("USER", "username");
define("PASS", "password");
define("DB", "dbname");
$link = mysqli_connect(HOST, USER, PASS) or die("Couldn't make database connection.");
$db = mysqli_select_db($link, DB) or die("Couldn't select database");
Strange things is that when I delete or die("Couldn't make database connection.") and or die("Couldn't select database"); then it's connect to db. Why ? Is there anything I'm doing wrong ?
This is the proposed way to handle the situation:
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
I am using a script php that connect to sqlserver but it doesn't works.
//connection to the database
$dbconn = mssql_connect($Server)
or die("Couldn't connect to SQL Server on $Server");
if($dbconn)echo 'Connected';
//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");
![when running script it gives this error, i m using windows authentication][1]
http://i.stack.imgur.com/a9ndN.jpg
Check mysql_connect()
$dbconn = mysql_connect('hostname','username','password');
Connect using mysql_connect()
$dbconn =mysql_connect(servername,username,password);
example
$con = mysql_connect("localhost","me","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
If I use the following code, it works:
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
But when I do this, it doesn't:
$db_host='localhost';
$db_id='root';
$db_pass='';
$con = mysql_connect($db_host, $db_id, $db_pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
Trying to swap (") and (').
mysql_ functions are discouraged for new applicationa you are advised to use mysqli or PDO. The following code uses PDO to connect to database.
//dependant on your setup
$host= "localhost";
$username="XXX";
$password="YYY";
$database="ZZZ";
// connect to the database
try {
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
//Remainder of code
}
catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]'). $e->getMessage()."\r\n", FILE_APPEND);//Change file name to suit
}
// close the connection
$dbh = null;
try using a script like this
$db_host = 'localhost';
$db_id = 'root';
$db_pass ='';
$con = mysql_connect ($ db_host, $ db_id, $db_pass) or die ('Could not connect:'. mysql_error ());
That code is fine. Review the error log- the problem has to be external to that code.
Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares