jmurphy1267
jmurphy1267

Reputation: 65

Cannot connect to local mysql

I am trying to connect to a database named BetaPoints. All of the credentials are located in a php file name EstablishAccess.php in the format of

<?php
  define (DB_USER, "BetaPoints");
  define (DB_PASSWORD, "password!");
  define (DB_DATABASE, "BetaPoints");
  define (DB_HOST, "hostname");
?>

I am trying to connect to the database with this

$connctn=mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD)
or die("cannot connect to database");
mysql_selectdb('BetaPoints')
or die('cannot select database');

I am getting this error:

Notice: Undefined variable: DB_HOST in /home/content/06/8274306/html/beta/mysuperscript.php on line 6

Notice: Undefined variable: DB_USER in /home/content/06/8274306/html/beta/mysuperscript.php on line 6

Notice: Undefined variable: DB_PASSWORD in /home/content/06/8274306/html/beta/mysuperscript.php on line 6

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/06/8274306/html/beta/mysuperscript.php on line 6 cannot connect to database

I have look for mistakes in my variables and I have logged in multiple times with the credentials that I have defined to those variables and I am still unable to find the mistake.

It says that I can not connect to local MySQL database when to my knowledge it is not a local database.

If any one has any suggestion post them below. I am trying to get help finding more information about what is going wrong and any solutions that I can do to make it work.

Upvotes: 2

Views: 1114

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

Edit: Firstly, you are declaring constants, so there's no need for $ variables syntax.

$connctn=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)

So make sure that you're using the correct data for all your constants.

Also, your syntax for mysql_selectdb('BetaPoints') is incorrect.

It's missing an underscore between select and db.

Use either mysql_select_db('BetaPoints') or mysql_select_db('BetaPoints', $connctn)

Example as per the manual:

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?>

However, mysql_ functions are deprecated and will be removed from future releases.

Use die('Not connected : ' . mysql_error()) instead of or die('cannot select database')


As per your edit:

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/06/8274306/html/beta/mysuperscript.php on line 6 cannot connect to database

You're including and mixing mysqli_ functions with mysql_ somewhere that you have not told us how you're using those.

  • Those different APIs do NOT intermix with each other.

Use one MySQL API only; not both.

Refer to the manual:

Example from the mysqli_connect() manual:

<?php
//conection:
$link = mysqli_connect("myhost","myuser","mypassw","mydb") 
or die("Error " . mysqli_error($link));

//consultation:

$query = "SELECT name FROM mytable" 
or die("Error in the consult.." . mysqli_error($link));

//execute the query.

$result = mysqli_query($link, $query);

//display information:

while($row = mysqli_fetch_array($result)) {
  echo $row["name"] . "<br>";
}
?> 

Upvotes: 3

MarkSkayff
MarkSkayff

Reputation: 1374

You have defined constants and then called them as variables. That's the mistake. DB_USER, DB_PASSWORD, DB_DATABASE, DB_HOST are constants and should be used like: DB_USER and not $DB_USER.

Also I recommend declaring them using quotes like this:

define ("DB_USER", "BetaPoints");

Then also, as another user commented first, mysql_selectdb syntax is wrong. The correct syntax is mysql_select_db ("db_name");

Upvotes: 0

Related Questions