Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO:为什么这会导致错误?_Php_Pdo - Fatal编程技术网

Php PDO:为什么这会导致错误?

Php PDO:为什么这会导致错误?,php,pdo,Php,Pdo,我不明白为什么在创建这样的对象时出现此错误: 错误出现在index.php中的这一行: $dbPerfiles = new DB_Functions(); 错误是: PDO Connection error: invalid data source name config.php <?php define ("DB_USER","root"); define ("DB_PASS","root"); define ("DNS","mysql:host=localhost;dbname=e

我不明白为什么在创建这样的对象时出现此错误:

错误出现在index.php中的这一行:

$dbPerfiles = new DB_Functions();
错误是:

PDO Connection error: invalid data source name
config.php

<?php
define ("DB_USER","root");
define ("DB_PASS","root");
define ("DNS","mysql:host=localhost;dbname=example");
?>
<?php
require_once 'config.php';
class DB_Connect {
    private static $_instance;

    //Connecting to database
    public function &pdo_connect() {    
        if(!self::$_instance) {
            try{
                self::$_instance = new PDO(DNS,DB_USER, DB_PASS);
                self::$_instance->setAttribute(PDO::ATTR_PERSISTENT, true);
                self::$_instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $ex) {
                die("PDO Connection error: ".$ex->getMessage()."<br/>");
            }
        }
        return self::$_instance;
    }

    private function __construct() {
    }

    private function __clone() {
    }
}
?>
<?php
    session_start();
    require_once 'DB_Connect.php';

    class DB_Functions extends DB_Connect{

        private $dbConnect = "";

        public function __construct() {
            $this->dbConnect = $this->pdo_connect();
        }

        public function __destruct() {
            $this->dbConnect = null;
        }

        public function getDetails() {
            try {
                //sql statement

            } catch (PDOException $e) {
                echo "Error: ".$e->getMessage()."<br/>";
                return false;
            }
        }
    }
?>
<?php 
session_start();
$max_time = 1800;
$current = time();
if(!isset($_SESSION['clientmac']['un']) ) { 
    $_SESSION['clientmac']['un'] == "";
    header('Location: index.php');
} else {
    if (!isset($_SESSION['timeLogin'])){
        $_SESSION['clientmac']['tl'] = time();
    } else {
        $session_life = $current - $_SESSION['clientmac']['tl'];    

        if ($session_life > $max_time) {
            header('Location: include/logout.php');
        } else {
            $_SESSION['clientmac']['tl'] = time();
        }
    }

    require_once 'include/DB_Functions.php';
    $dbPerfiles = new DB_Functions();  //With this line shows the error

    //code to connect to getDetails() function in DB_Functions.php and
    //retrieve some data.

?>
<!doctype html>
<html lang=en>
<!--
CODE HTML
-->
</html>
<?php
}
?>

DB_Connect.php

<?php
define ("DB_USER","root");
define ("DB_PASS","root");
define ("DNS","mysql:host=localhost;dbname=example");
?>
<?php
require_once 'config.php';
class DB_Connect {
    private static $_instance;

    //Connecting to database
    public function &pdo_connect() {    
        if(!self::$_instance) {
            try{
                self::$_instance = new PDO(DNS,DB_USER, DB_PASS);
                self::$_instance->setAttribute(PDO::ATTR_PERSISTENT, true);
                self::$_instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $ex) {
                die("PDO Connection error: ".$ex->getMessage()."<br/>");
            }
        }
        return self::$_instance;
    }

    private function __construct() {
    }

    private function __clone() {
    }
}
?>
<?php
    session_start();
    require_once 'DB_Connect.php';

    class DB_Functions extends DB_Connect{

        private $dbConnect = "";

        public function __construct() {
            $this->dbConnect = $this->pdo_connect();
        }

        public function __destruct() {
            $this->dbConnect = null;
        }

        public function getDetails() {
            try {
                //sql statement

            } catch (PDOException $e) {
                echo "Error: ".$e->getMessage()."<br/>";
                return false;
            }
        }
    }
?>
<?php 
session_start();
$max_time = 1800;
$current = time();
if(!isset($_SESSION['clientmac']['un']) ) { 
    $_SESSION['clientmac']['un'] == "";
    header('Location: index.php');
} else {
    if (!isset($_SESSION['timeLogin'])){
        $_SESSION['clientmac']['tl'] = time();
    } else {
        $session_life = $current - $_SESSION['clientmac']['tl'];    

        if ($session_life > $max_time) {
            header('Location: include/logout.php');
        } else {
            $_SESSION['clientmac']['tl'] = time();
        }
    }

    require_once 'include/DB_Functions.php';
    $dbPerfiles = new DB_Functions();  //With this line shows the error

    //code to connect to getDetails() function in DB_Functions.php and
    //retrieve some data.

?>
<!doctype html>
<html lang=en>
<!--
CODE HTML
-->
</html>
<?php
}
?>

DB_Functions.php

<?php
define ("DB_USER","root");
define ("DB_PASS","root");
define ("DNS","mysql:host=localhost;dbname=example");
?>
<?php
require_once 'config.php';
class DB_Connect {
    private static $_instance;

    //Connecting to database
    public function &pdo_connect() {    
        if(!self::$_instance) {
            try{
                self::$_instance = new PDO(DNS,DB_USER, DB_PASS);
                self::$_instance->setAttribute(PDO::ATTR_PERSISTENT, true);
                self::$_instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $ex) {
                die("PDO Connection error: ".$ex->getMessage()."<br/>");
            }
        }
        return self::$_instance;
    }

    private function __construct() {
    }

    private function __clone() {
    }
}
?>
<?php
    session_start();
    require_once 'DB_Connect.php';

    class DB_Functions extends DB_Connect{

        private $dbConnect = "";

        public function __construct() {
            $this->dbConnect = $this->pdo_connect();
        }

        public function __destruct() {
            $this->dbConnect = null;
        }

        public function getDetails() {
            try {
                //sql statement

            } catch (PDOException $e) {
                echo "Error: ".$e->getMessage()."<br/>";
                return false;
            }
        }
    }
?>
<?php 
session_start();
$max_time = 1800;
$current = time();
if(!isset($_SESSION['clientmac']['un']) ) { 
    $_SESSION['clientmac']['un'] == "";
    header('Location: index.php');
} else {
    if (!isset($_SESSION['timeLogin'])){
        $_SESSION['clientmac']['tl'] = time();
    } else {
        $session_life = $current - $_SESSION['clientmac']['tl'];    

        if ($session_life > $max_time) {
            header('Location: include/logout.php');
        } else {
            $_SESSION['clientmac']['tl'] = time();
        }
    }

    require_once 'include/DB_Functions.php';
    $dbPerfiles = new DB_Functions();  //With this line shows the error

    //code to connect to getDetails() function in DB_Functions.php and
    //retrieve some data.

?>
<!doctype html>
<html lang=en>
<!--
CODE HTML
-->
</html>
<?php
}
?>

index.php

<?php
define ("DB_USER","root");
define ("DB_PASS","root");
define ("DNS","mysql:host=localhost;dbname=example");
?>
<?php
require_once 'config.php';
class DB_Connect {
    private static $_instance;

    //Connecting to database
    public function &pdo_connect() {    
        if(!self::$_instance) {
            try{
                self::$_instance = new PDO(DNS,DB_USER, DB_PASS);
                self::$_instance->setAttribute(PDO::ATTR_PERSISTENT, true);
                self::$_instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $ex) {
                die("PDO Connection error: ".$ex->getMessage()."<br/>");
            }
        }
        return self::$_instance;
    }

    private function __construct() {
    }

    private function __clone() {
    }
}
?>
<?php
    session_start();
    require_once 'DB_Connect.php';

    class DB_Functions extends DB_Connect{

        private $dbConnect = "";

        public function __construct() {
            $this->dbConnect = $this->pdo_connect();
        }

        public function __destruct() {
            $this->dbConnect = null;
        }

        public function getDetails() {
            try {
                //sql statement

            } catch (PDOException $e) {
                echo "Error: ".$e->getMessage()."<br/>";
                return false;
            }
        }
    }
?>
<?php 
session_start();
$max_time = 1800;
$current = time();
if(!isset($_SESSION['clientmac']['un']) ) { 
    $_SESSION['clientmac']['un'] == "";
    header('Location: index.php');
} else {
    if (!isset($_SESSION['timeLogin'])){
        $_SESSION['clientmac']['tl'] = time();
    } else {
        $session_life = $current - $_SESSION['clientmac']['tl'];    

        if ($session_life > $max_time) {
            header('Location: include/logout.php');
        } else {
            $_SESSION['clientmac']['tl'] = time();
        }
    }

    require_once 'include/DB_Functions.php';
    $dbPerfiles = new DB_Functions();  //With this line shows the error

    //code to connect to getDetails() function in DB_Functions.php and
    //retrieve some data.

?>
<!doctype html>
<html lang=en>
<!--
CODE HTML
-->
</html>
<?php
}
?>

我希望连接到DB_函数,并使用getDetails()函数或其他函数进行连接 在该文件中,并检索数据。。只有那个

我希望我已经解释过了


问候

诸如无效数据源名称之类的错误可能涉及:

  • DSN语法不正确(在您的情况下,它看起来是正确的)
  • DSN参数不正确
  • 主机可能不是localhost(您的mysql配置可能不允许通过TCP或localhost intself进行连接,但我对此表示怀疑,因为默认情况下允许从localhost进行TCP连接)
  • 数据库名称可能不正确(因为在您的代码中是
    示例
    ),这使我认为您可能没有创建名为example的数据库

  • 解决了!只是我必须加上这一行

    require_once 'include/config.php'; 
    
    在上面

    require_once 'include/DB_Functions.php'; 
    
    而且有效,但我不明白为什么


    因为在DB_Connect.php中,这一行是相同的。

    您得到的错误是什么?写在标题中:PDO连接错误:无效的数据源名称为什么您得到一个
    DNS
    作为PDO构造函数的参数?@SoldierCorp哦!很抱歉现在更清楚了!;)@arxanas这是数据源名称、DSN和连接字符串的拼写错误,看起来像
    mysql:host=localhost;dbname=my_database
    。在dbname中,我仅以“example”为例,但我的dbname是mac_system。关于“localhost”。。我只是放了一部分代码,但我有很多函数,工作很好!但只有在index.php中导入或需要DB_函数时,才会显示该错误!对于实际存在的数据库,您应该使用真实的数据库名称。PDO构造函数试图连接到MySQL数据库,这意味着数据库不存在,无法连接并触发错误。解决了它!只是我不得不添加这一行require_once'include/config.php';以上要求_once“include/DB_Functions.php”;而且有效,但我不明白为什么?因为在DB_Connect.php中是同一行。。