试图了解为什么要使用此类型的数据库设置

编程入门 行业动态 更新时间:2024-10-28 22:25:51
本文介绍了试图了解为什么要使用此类型的数据库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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");

更多推荐

试图了解为什么要使用此类型的数据库设置

本文发布于:2023-10-24 09:10:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要使   类型   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!