试图了解为什么要使用此类型的数据库设置 [英] Trying to understand why you would use this type of database setup

查看:96
本文介绍了试图了解为什么要使用此类型的数据库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了以这种方式设置的数据库。

I recently came across a database being set up in this manner.

<?php
ini_set( "display_errors", true );
date_default_timezone_set( "Australia/Sydney" );  // http://www.php.net/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:

http://pastebin.com/ 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");

这篇关于试图了解为什么要使用此类型的数据库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆