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.

3
  • 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 <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. If you get a deprecation notice, then you'll know what to use instead. Commented Mar 1, 2015 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? Commented Mar 1, 2015 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. Commented Mar 1, 2015 at 20:34

2 Answers 2

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>"; } ?> 
Sign up to request clarification or add additional context in comments.

8 Comments

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.
This Q&A on Stack will explain it better stackoverflow.com/q/3633900 @user3600462
@user3600462 wait I noticed another error. I will edit my answer. Give me a minute or so.
@user3600462 Reload my answer. You are using constants, so there's no need for $ signs.
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
|
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");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.