PHP不包括连接信息,有什么问题?

时间:2010-10-13 18:59:10

标签: php mysql include

我正在尝试推出一个CMS网站,我正在1和1互联网托管。我正在尝试连接到我的MySQL数据库,我收到以下错误:

  

无法通过socket'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)

经过一番挫折后,我决定检查我的include,结果发现以下代码不包括我的连接变量文件。

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

?>

怎么了?

4 个答案:

答案 0 :(得分:4)

问题出在你的connection.php文件中,其中有一个拼写错误:

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

修复(以及Josh's answer中提到的include()问题)应解决您的问题。

答案 1 :(得分:1)

确认您已正确设置路径

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

检查connection.php的权限,测试它是否可读

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

MySQL数据库是否在同一台服务器上? AKA Localhost还是其他服务器?

硬编码路径

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

编辑: 你能比较connection.php和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 />";

答案 2 :(得分:1)

经过一番挫折之后,我决定检查我的include,结果证明以下代码不包括我的连接变量文件

要确定是否确实如此,请尝试以下操作:

  1. 在connection.php中添加以下行:die('This is connection.php.');
    查看脚本是否已死亡。如果是,则包含该文件。
  2. $link = mysql_connect($connection_server, $connection_user, $connection_password)之前添加:
    var_dump($connection_server)
    并查看运行脚本时是否输出连接服务器,或者而是出现“NULL”。如果它为null,则表示该变量未被设置。

  3. 编辑1

    根据your message in chat

    您不能包含使用http://your.domain/connection.php之类的远程文件。嗯,你可以,但正如你所看到的,它将无法正常工作。包括( “HTTP://your.domain/new/connection.php”);表示“将connection.php作为单独的请求执行并包含它的输出”。

    你想:

    include(dirname(__FILE__)."/connection.php");
    

答案 3 :(得分:1)

  

输入error_reporting(E_ALL);之前   mysql_connect()函数。你有通知吗?   关于未定义的变量? -   Lekensteyn


  @Lekensteyn - 是的,我知道。 - Moshe

config.php之前的$connections_included中添加以下内容。

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

它会将这些变量导出到全局范围。