PHP does not include connection information, what's wrong?

I am trying to launch a CMS website and I am on 1and1 internet hosting. I am trying to connect to my MySQL database and I am getting the following error:

Unable to connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

After some disappointment, I decided to check my inclusion, and it turned out that the following code does not include my connection variable file.

admin.php

<?php include("connection.php"); $link = mysql_connect($connection_server, $connection_user, $connection_password); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> 

connection.php

 <?php /* -------------------------------------- / / Bitachon.org Connection Variables / / Created by: Moshe [Last Name] / / / Date: October 12, 2010 / / / This file contains the connection / variable to connect to the MySQL / database that powers [site] / / --------------------------------------*/ //Variable to confirm that the file has been included $connnections_included = true; /* --- Connection Variables ---*/ $connnection_server = "[server]"; $connection_database = "[db]"; $connection_user = "[username]"; $connection_password = "[password]"; ?> 

What happened?

+4
source share
4 answers

The problem is in the connection.php file, where there is a typo:

 $connnection_server = "[server]"; // ^-- third n 

Fixing that (along with the include() problem mentioned in Josh's answer ) should solve your problem.

+4
source

Make sure you have the path set correctly.

 include("/path/to/connection.php"); 

Check permissions on connection.php, check if it is readable

 $filename = 'connection.php'; if(is_readable($filename)) { echo 'The file is readable'; } else { echo 'The file is not readable'; } 

Is the MySQL database on a single server? AKA Localhost or another server?

Hard code: path

 $pwd = `pwd`; echo "PWD: ".$pwd."<br />"; // use just for testing include($pwd."/connection.php"); 

EDIT: Can you compare connection.php and admin.php

 $filename = 'admin.php'; echo "Permissions: ".substr(sprintf("%o",fileperms($filename)),-4)."<br />"; echo "File Owner: ".fileowner($filename)."<br />"; echo "File Group: ".filegroup($filename)."<br />"; if(is_executable($filename)) { echo ("$filename is executable<br />"); } else { echo ("$filename is not executable<br />"); } if(is_readable($filename)) { echo "$filename is readable<br />"; } else { echo "$filename is not readable<br />"; } echo "Real Path: ".realpath($filename)."<br />"; $filename = 'connection.php'; echo "Permissions: ".substr(sprintf("%o",fileperms($filename)),-4)."<br />"; echo "File Owner: ".fileowner($filename)."<br />"; echo "File Group: ".filegroup($filename)."<br />"; if(is_executable($filename)) { echo ("$filename is executable<br />"); } else { echo ("$filename is not executable<br />"); } if(is_readable($filename)) { echo "$filename is readable"; } else { echo "$filename is not readable"; } echo "Real Path: ".realpath($filename)."<br />"; 
+1
source
After some disappointment, I decided to check my included option and it turned out that the following code does not include my connection variable file

To determine if this is true, try the following:

  • In connection.php add the line: die('This is connection.php.');
    See if the script dies. If so, the file is included.
  • Before using $link = mysql_connect($connection_server, $connection_user, $connection_password)
    add:
    var_dump($connection_server)
    and see if the connection server is displayed when the script starts, or instead something like "NULL" appears. If it is zero, you know that the variable is not set.

EDIT 1 :

How is your chat message :

You cannot include a remote file, for example, using http://your.domain/connection.php . Well, you can, but as you have seen, this will not work. include ("http: //your.domain/new/connection.php"); means "execute connection.php as a separate request and enable its output."

Do you want to:

 include(dirname(__FILE__)."/connection.php"); 
+1
source

Put error_reporting (E_ALL); to mysql_connect (). Do you get undefined variable notifications? - Lekensteyn


@Lekensteyn - Yes, I know. - Moshe

Put the following in config.php , before $connections_included .

 global $connections_included, $connection_server, $connection_user, $connection_password; 

It exports these variables to the global scope.

+1
source

All Articles