mysqli、OOP 与程序化

2021-12-25 00:00:00 php pdo mysqli

我第一次看到这个.我不知道你可以通过做类似的事情来实例化一个 mysqli 类

I just saw this for the first time. I had no idea you can instantiate a mysqli class by doing something like

new mysqli( $host, $username, $password, $db );

这对我来说是全新的...连接到数据库时,我在网上或书中看到的每个教程都是这样的:

This is brand new to me...Every tutorial I have seen online or in books when connecting to database does something like this:

$conn = mysqli_connect( $host, $username, $password, $db );

if ( !$conn ) {
    die( 'Sorry, could not connect');
}

为什么大多数教程都以程序方式进行操作?即使在我看到的有关创建单独的数据库类的教程中,也告诉我使用程序样式代码...

Is there a reason why most tutorials do it the procedural way? Even in tutorials I have seen on creating a separate database class, tell me to use the procedural style code...

mysqli procedural vs oop 有什么区别?有没有?我是否必须安装某种扩展,或者我可以开始在我的代码中使用吗?这里完全是新手,所以不太确定.

What's the difference with mysqli procedural vs oop? Is there any? Do I have to install any extensions of some kind or can I just start using in my code? Total newb here, so not really sure.

另外,什么时候使用 PDO 和 mysqli 比较好?处理大型企业应用程序的开发人员最常使用 PDO 吗?或者只是喜好问题.

Also, when is it a good idea to use PDO vs mysqli? Is PDO used most commonly by developers that work on large scale Enterprise applications? Or is simply a matter of preference.

感谢您的帮助...

学习 php 很快就上瘾了……我只是想知道的越来越多.

learning php has quickly become addictive...I just want to know more and more.

推荐答案

互联网上 99% 的 PHP 教程已经过时,提倡糟糕的代码,或者是由可能不应该教 PHP 的人编写的(通常包括上述所有内容)).您应该找到一些好的.

99% of PHP tutorials on the internet are outdated, advocate terrible code or are written by people that probably shouldn't teach PHP (and commonly all of the above). You should find the handful of good ones.

PDO 通常比 mysqli 受到开发人员的青睐,包括我.一方面,它可以处理比 MySQL 更多的数据库.

PDO is generally favoured over mysqli by developers, including me. For one, it can handle many more databases than just MySQL.

相关文章