tags:

views:

84

answers:

4

I'm trying to roll a CMS website and I'm on 1and1 Internet's hosting. I'm trying to connect to my MySQL database and I get the following error:

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

After some frustration, I decided to check my include and it turns out that the following code is not including my connection variables 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's wrong?

+1  A: 

Verify that you have the path set correctly

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

Check permissions on connection.php, test to see if it's 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 the same server? AKA Localhost or another server?

Hard code the 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 />";
Phill Pafford
They are in the same folder.
Moshe
can we see what's in connection.php? minus credentials
Phill Pafford
is this for the example or are the values enclosed in [] ? $connnection_server = "server";$connection_database = "db";$connection_user = "username";$connection_password = "password";
Phill Pafford
@Phil Pafford- I added the brackets for the example. The real values are in the live file.
Moshe
in your edit you have admin.php, try include('admin.php');
Phill Pafford
Your code returns - *is* readable.
Moshe
@Phil - I don't understand that last bit. Where do I add the include?
Moshe
The database is on another server.
Moshe
try require("connection.php"); this should stop the script execution on error
Phill Pafford
@Phil - not working. It seems to be ignoring include statements.
Moshe
@Moshe I saw you have admin.php in your edit code above, thought you might had put that by mistake but you had corrected the edit above to read connection.php. Just thought you might replace connection.php with admin.php, but try my suggestion above
Phill Pafford
what's the error on the require('connection.php') display?
Phill Pafford
Nothing, just the undefined variable error.
Moshe
Just for kicks can you hard code the path to the include script? include(/hard/code/path/to/connection.php');
Phill Pafford
Phil - Yep, just for kicks. No, it doesn't help.
Moshe
interesting thread that might help: http://www.dmcinsights.com/phorum/read.php?9,28748
Phill Pafford
added a test to compare files and permission
Phill Pafford
+1  A: 
After some frustration, I decided to check my include and it turns out that the following code is not including my connection variables file

To determine if this is really the case, try the following:

  1. In connection.php add the line: die('This is connection.php.');
    See if the script dies. If so, the file is being included.
  2. Before
    $link = mysql_connect($connection_server, $connection_user, $connection_password)
    add:
    var_dump($connection_server)
    and see if the connection server is output when you run the script, or if something like "NULL" appears instead. If it's null, you know the variable isn't being set.

EDIT 1:

As per your message in chat:

You cannot include a remote file like using http://your.domain/connection.php. Well you can but as you saw, it won't work. include("http://your.domain/new/connection.php"); means "execute connection.php as a seperate request and include it's output".

You want:

include(dirname(__FILE__)."/connection.php");
Josh
And you should be happy that it works in that way. If your script can get credentials in that way, everyone else can do that.
Lekensteyn
I am editing my post back and forth, because [what's happening here is unclear](http://chat.meta.stackoverflow.com/transcript/message/235682#235682)...
Josh
+1  A: 

Put error_reporting(E_ALL); before mysql_connect(). Do you get notices about undefined variables? – Lekensteyn


@Lekensteyn - Yes, I do. – Moshe

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

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

It'll export those variables to the global scope.

Lekensteyn
+4  A: 

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

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

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

Tim Stone
Lol, yep. Thanks for help at the Roach Motel. (add the bit about the `http://` please.
Moshe