【问题标题】:Including php file with database connection error包含带有数据库连接错误的 php 文件
【发布时间】:2013-11-23 07:43:52
【问题描述】:

我想要一个包含所有数据库信息(数据库名称、主机、用户名、密码)的通用 php 文件

但是当我包含页面时,在 index.php 中我得到了这个错误:

拒绝用户 'apache'@'localhost' 访问(使用密码:否)

connect.php

<?php
class dbconnect{
    private $host = '**'; 
    private $user = '**';
    private $pass = '**';
    public $con;

function Connect($db = '**') {

    if($db=='**'){
        $this->host="localhost";
        $this->user="**";
        $this->pass="**";
        $db="**";
    }else{
                $this->host="**";
                $this->user="**";
                $this->pass="**";
    }

    $this->con = mysql_connect($this->host,$this->user,$this->pass);
    if (!$this->con)
      {
        die('Could not connect: ' . mysql_error());
      }
    $blaa = mysql_select_db($db, $this->con);
    mysql_query("SET NAMES UTF8");
    return $blaa;
}

function Disconnect() {
    //$this->con = mysql_connect($this->host,$this->user,$this->pass);
    mysql_close();
}
}
?>

我确定**信息是正确的,因为当我将其指定为:

$con=mysqli_connect("example.com","example","password","my_db");

在 index.php 中有效

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    如果连接信息正确,请检查您的 MySQL 用户的主机访问权限:

    SELECT user, host FROM mysql.user
    

    如果设置了“localhost”,则用户只能在本地访问数据库,否则“%”将打开访问。

    【讨论】:

      【解决方案2】:

      通常这是连接凭据的问题。

      检查您是否可以使用您为该网站设置的详细信息登录到您的 mysql。

      mysql -u apache -p

      然后它会要求你输入密码。

      如果该登录无效,则说明该用户的 mysql 帐户有问题。

      【讨论】:

        【解决方案3】:

        您使用不正确的参数进行访问。只需转储 $this-&gt;con = mysql_connect($this-&gt;host,$this-&gt;user,$this-&gt;pass); 行的变量即可。您可以使用调试器或echo打印指令。

        进一步使用 PDO 扩展来访问数据库。更好! Why shouldn't I use mysql_* functions in PHP?

        【讨论】:

        • 您确定要恢复超过五年的问题吗? ;)
        • 哎呀。对不起,伙计:)
        • 丹吉特。没有意识到这是一个复兴,我完全写了一个回应。哈哈,小屁孩。
        【解决方案4】:

        请务必注意,您的测试用例实际上并不能证明它有效。

        这个输出是什么:

        $conn = mysql_connect("example.com", "user", "password");
        if (!$conn) {
            die('Could not connect: ' . mysql_error());
        }
        

        因为如果没有它,您不一定会获得失败的信息。

        最重要的是,为了调试目的,让我们稍微简化一下你的类:

        class dbconnect
        {
            private $host = '**';
            private $user = '**';
            private $pass = '**';
            public $con;
        
            public function Connect($host = "localhost", $user = "root", $pass = "")
            {
                $this->host = $host;
                $this->user = $user;
                $this->pass = $pass;
        
                $this->con = mysql_connect($this->host, $this->user, $this->pass);
                if (!$this->con) {
                    die('Could not connect: ' . mysql_error());
                }
        
                $blaa = mysql_select_db($db, $this->con);
                mysql_query("SET NAMES UTF8");
        
                return $blaa;
            }
        
            public function Disconnect()
            {
                mysql_close($this->con);
            }
        }
        

        现在你会得到什么

        $db = new dbconnect("example.com", "user", "password");
        

        确保您使用的凭据有效,并且您没有通过这些方法遇到默认值或不正确的变量分配等问题。

        现在,如果您不想提供这些值,您可以:

        $db = new dbconnect();
        

        公共服务公告

        查看 PHP 的 PDOminimum(但实际上,只需使用 PDO)mysqli 替代方案。 PHP 的 mysql 扩展是安全的,你永远不应该在任何环境中使用它。

        【讨论】:

          猜你喜欢
          • 2014-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多