在面向对象的 PHP 中连接到数据库

发布于 2024-08-21 00:56:26 字数 164 浏览 2 评论 0原文

如何在 oo php 中创建一个类,其中该类将管理数据库连接?

示例代码或网站即可。 顺便说一句,我正在使用 MySQL Command Client。

主要问题: 如何连接到数据库以便插入新记录、检索记录、更新记录?

谢谢你!

How can I create a class in oo php wherein that class will be the one that will manage the DB connection?

Sample codes or sites will do.
Im using MySQL Command Client by the way.

Main problem:
How can I connect to the DB so that I can insert new records, retrieve records, update records?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疏忽 2024-08-28 00:56:26

您正在寻找 MySQLi。它是自 PHP 5 以来的内置扩展,您可以使用它来查询 MySQL 数据库中的数据。

$mysql = new MySQLi('localhost', 'username', 'password', 'database')
$mysql->query("SELECT * FROM users");

You're looking for MySQLi. It's a built-in extension since PHP 5 that you may use to query a MySQL database for data.

$mysql = new MySQLi('localhost', 'username', 'password', 'database')
$mysql->query("SELECT * FROM users");
菊凝晚露 2024-08-28 00:56:26

如果您从头开始构建应用程序,我建议使用 PHP MVC 框架包,例如 CakePHP 或 CodeIgniter。它们都包含一个数据库抽象层,可以标准化标准数据库功能,并使您不必编写 SQL 语句。这样做的好处在于,它还允许您稍后通过简单的配置更改来更改数据库类型,从 MySQL 更改为 PostgreSQL。

如果您无法使用 MVC,请查看 ADODB,它是一个独立的数据库抽象类:

http://adodb。 sourceforge.net/

If you're build an app from scratch, I would recommend using a PHP MVC framework package, like CakePHP or CodeIgniter. They both include a database abstraction layer which normalizes standard database functionality and keeps you from having to write SQL statements. The beauty of this is that it also allows you to change your db type later, from MySQL to say PostgreSQL, with a simple configuration change.

If you can't use an MVC, check out ADODB, which is a standalone database abstraction class:

http://adodb.sourceforge.net/

扭转时空 2024-08-28 00:56:26

我推荐 PHP 的 PDO(PHP 数据对象)扩展。它有许多优点,包括能够使用相同的代码库访问多种数据库类型,包括 MySQL、SQLite 和 PostgreSQL。我将它与扩展 PDO 的轻量级数据库类一起使用。您可以在 Google 项目托管上找到此开源项目,网址为 http://code。 google.com/p/php-pdo-wrapper-class

PHP's PDO (PHP Data Objects) extension is my recommendation. It has many upsides including the ability to access multiple database types including MySQL, SQLite, and PostgreSQL with the same codebase. I use it alongside a light-weight database class that extends PDO. You can find this open-source project on Google's Project Hosting at http://code.google.com/p/php-pdo-wrapper-class.

笑叹一世浮沉 2024-08-28 00:56:26

为什么不使用 mysql 包装器。它具有添加/更新/删除功能以及更多实用功能,可将您的开发时间缩短约40%。当然,它是完全面向对象的解决方案。

Why don't you use a mysql wrapper. It has functions to add/update/delete and a lot more utitlity functions to fasten your development time by about 40%. Ofcourse it is fully object-oriented solution.

撞了怀 2024-08-28 00:56:26

wmpjmurray提到的Adodb是一个不错的选择。您也可以创建自己的类来扩展 PDO。 PHP 手册中有一些示例说明其他人如何完成此操作。

Adodb mentioned by wmpjmurray is a good choice. You could also just create your own class that extends PDO. There are a few examples of how other people have done this in the PHP Manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文