如何正确设置PDO连接
我就是这样做的:
public_html/* index.php * initialize/ -- load.initialize.php -- configure.php -- sessions.php
index.phprequire('initialize/load.initialize.php');
.
load.initialize.php
# site configurations require('configure.php');# connect to database require('root/somewhere/connect.php'); // this file is placed outside of public_html for better security.# include classes foreach (glob('assets/classes/*.class.php') as $class_filename){ include($class_filename); }# include functions foreach (glob('assets/functions/*.func.php') as $func_filename){ include($func_filename); }# handle sessions require('sessions.php');
autoload
figre.php
connect.php
class connect_pdo{ protected $dbh; public function __construct() { try { $db_host = ' '; // hostname $db_name = ' '; // databasename $db_user = ' '; // username $user_pw = ' '; // password $con = new PDO('mysql:host='.$db_host.'; dbname='.$db_name, $db_user, $user_pw); $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $con->exec("SET CHARACTER SET utf8"); // return all sql requests as UTF-8 } catch (PDOException $err) { echo "harmless error message if the connection fails"; $err->getMessage() . "<br/>"; file_put_contents('PDOErrors.txt',$err, FILE_APPEND); // write some details to an error-log outside public_html die(); // terminate connection } }
烙印99