简体   繁体   English

使用 php 配置文件连接到数据库而不是 PDO 连接线

[英]Using php config file to connect to database instead of PDO connection line

I have the following lines Of code for a database connection, however I would just want to use my include("config.php") Instead of this long line of code, how would i go about doing that我有以下几行用于数据库连接的代码,但是我只想使用我的 include("config.php") 而不是这一长行代码,我 go 如何做到这一点

try {
    $dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
    die('unable to connect to database ' . $e->getMessage());
}    

// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh); 

Just make a config.php file, for example consider this file is stored in web root ( /var/www/ ), then the contents of the config.php are like this:只需创建一个 config.php 文件,例如假设此文件存储在 web 根目录 ( /var/www/ ),则 config.php 的内容如下:

<?php

    try {
        $dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
    }
    catch (PDOException $e) {
        die('unable to connect to database ' . $e->getMessage());
    }    

    // create LM object, pass in PDO connection
    $lm = new lazy_mofo($dbh);

?>

then in your other PHP scripts use the include function:然后在您的其他 PHP 脚本中使用include function:

<?php

    include '/var/www/config.php';
    ...

Now in your PHP scripts you can access the $lm object and $dbh object.现在,在您的 PHP 脚本中,您可以访问$lm object 和$dbh object。

for example include("config.inc.php") and for the config.inc.php:例如include("config.inc.php")和 config.inc.php:

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
    die('unable to connect to database ' . $e->getMessage());
}
$lm = new lazy_mofo($dbh); 
?>

You can create a db wrapper class in your include file and get really lazy about it =)你可以在你的包含文件中创建一个 db wrapper class 并且真的很懒惰=)

    define('DB_TYPE', 'mysql');
    define('DB_HOST', 'localhost');
    define('DB_NAME', 'lms');
    define('DB_USER', 'root');
    define('DB_PASS', 'password');

    class Database{

        public function __construct() {

            $db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }

    }

    $o = new Database();

my connection PDO 我的连接PDO

  define("DB_HOST", "localhost");
  define("DB_USER", "root");
  define("DB_PASS", "");
  define("DB_NAME", "db");

      try {

        $db = new PDO("mysql:host=" . DB_HOST . ";charset=utf8mb4;dbname=" . DB_NAME, DB_USER, DB_PASS);

        /* set the PDO error mode to exception */
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);                           

        //echo "Connected successfully"; 

      }

      catch(PDOException $e)    {

        echo "Connection failed: " . $e->getMessage();

      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM