Moshe
Moshe

Reputation: 58107

PHP not including connection info, what's wrong?

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?

Upvotes: 4

Views: 992

Answers (4)

Tim Stone
Tim Stone

Reputation: 19189

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.

Upvotes: 4

Josh
Josh

Reputation: 11070

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

Upvotes: 1

Lekensteyn
Lekensteyn

Reputation: 66515

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.

Upvotes: 1

Phill Pafford
Phill Pafford

Reputation: 85378

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

Upvotes: 1

Related Questions