2

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.

Community
  • 1
  • 1
jmurphy1267
  • 65
  • 1
  • 9
  • Pass an extra parameter to it `mysql_selectdb('BetaPoints', $connctn)` - Do use up-to-date APIs such as `mysqli_` or PDO. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Mar 01 '15 at 20:08
  • I added error reporting to the document and I am using require ("EstablishAccess.php"); to have this variable i need to connect transfer into the php file and the error reporting said they were not defined. Should I be using i different function to do what I need it to do? – jmurphy1267 Mar 01 '15 at 20:13
  • As per your edit, I did make an edit to my answer before you posted that. Reload it to see the changes I've made. But where is `mysqli_connect()` coming from? You cannot mix APIs like that. – Funk Forty Niner Mar 01 '15 at 20:34

2 Answers2

3

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>";
}
?> 
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Would it be better to use the include() or require() function to import my variables? I think that is why its not connecting since some how my database variable are not being defined. – jmurphy1267 Mar 01 '15 at 20:20
  • This Q&A on Stack will explain it better http://stackoverflow.com/q/3633900/ @user3600462 – Funk Forty Niner Mar 01 '15 at 20:23
  • @user3600462 wait I noticed another error. I will edit my answer. Give me a minute or so. – Funk Forty Niner Mar 01 '15 at 20:27
  • @user3600462 Reload my answer. You are using constants, so there's no need for `$` signs. – Funk Forty Niner Mar 01 '15 at 20:29
  • I still get the connection error? Warning: mysql_selectdb() [function.mysql-selectdb]: 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 8 Warning: mysql_selectdb() [function.mysql-selectdb]: A link to the server could not be established in /home/content/06/8274306/html/beta/mysuperscript.php on line 8 cannot select database – jmurphy1267 Mar 01 '15 at 20:37
  • @user3600462 I've made another edit, and reload it then read it in its entirety. You cannot mix `mysqli_` and `mysql_` functions together. Use one, not both. That should get you started. Look under **As per your edit...** – Funk Forty Niner Mar 01 '15 at 20:40
  • I learned alot from this answer, thank you @fred-ii- for the ans, it solved my problem too, Vup for this great ans.. – student Mar 02 '15 at 15:44
  • @student You're quite welcome. Glad to hear it has helped you also, *cheers!* – Funk Forty Niner Mar 02 '15 at 15:47
0

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");

MarkSkayff
  • 1,334
  • 9
  • 14