我最近遇到了以这种方式设置的数据库。
I recently came across a database being set up in this manner.
<?php ini_set( "display_errors", true ); date_default_timezone_set( "Australia/Sydney" ); // www.php/manual/en/timezones.php define( "DB_DSN", "mysql:host=localhost;dbname=cms" ); define( "DB_USERNAME", "root" ); define( "DB_PASSWORD", "" ); define( "CLASS_PATH", "classes" ); define( "TEMPLATE_PATH", "templates" ); define( "HOMEPAGE_NUM_ARTICLES", 5 ); define( "ADMIN_USERNAME", "admin" ); define( "ADMIN_PASSWORD", "mypass" ); require( CLASS_PATH . "/Article.php" ); function handleException( $exception ) { echo "Sorry, a problem occurred. Please try later."; error_log( $exception->getMessage() ); } set_exception_handler( 'handleException' ); ?>我理解它的工作原理,但我从来没有见过这样的连接设置,
I understand how its working but I've never seen a connection being set up like this, is there a reason why you would want to set up your database in this manner?
推荐答案这是一个很常见的情况。我创建了一个MySQL类,我在我的PHP项目中使用,并可以从任何地方调用它连接到数据库。您可以在这里抓取:
It's a very common scenario. I created a MySQL class that I use in my PHP projects, and can call it from anywhere to connect to the database. You can grab it here:
pastebin/ 2FrPHVLR
对于需要连接到我的数据库的任何页面,我加载一个需要该类的全局配置文件,并添加我的凭证, p>
For any pages that need to connect to my database, I load a global config file that requires that class, and add my credentials, like
$db_hostspec = 'localhost'; $db_database = 'mydb'; $db_username = 'root'; $db_password = 'MySekretPassw0rd'; $db_port = '3306';然后,我可以使用
$db = new MySQL($db_hostspec, $db_username, $db_password, $db_database);我可以使用以下命令调用数据库:
And I can make calls to the database with:
$products = $db->execute("SELECT * FROM Products");更多推荐
试图了解为什么要使用此类型的数据库设置
发布评论