Hướng dẫn can i use both pdo and mysqli? - tôi có thể sử dụng cả pdo và mysqli không?
IntroductionLong gone are the days of using the 5 extension, as its methods have been deprecated since PHP 5.5 and removed as of PHP 7. Alas, the internet is still plagued with a ton of old
tutorials that beginners will simply copy/paste and use on a shared hosting platform with an older PHP version, thus continuing its legacy. Show
If you are using MySQL or MariaDB in PHP, then you have the ability to choose either MySQLi or PDO. The former is simply an improved version with procedural and OOP support and added prepared statements, while the latter is an abstraction layer that allows you to use a unified API for all 12 database drivers it supports. Though it should be mentioned that MySQL is undoubtedly the most popular database to use in the PHP world anyway. In theory, one might assume the discussion should be over. We don't need a vendor-specific API for every type of database that exists, as it's so much simpler to use just one. While there's certainly a lot of truth to this, the problem is that 6 simply
doesn't have all of the newest and most advanced features MySQLi does. I honestly don't understand why this is the case, as it would completely eliminate any reason to use the vendor-specific API. That said, I'd imagine that most people don't need these extra features, but there are definitely some who do.For anyone who's interested, here's a full writeup of PDO and MySQLi. PDO AdvantagesMySQLi has quite a few aspects to play catchup with PDO. It needs to adds features like:
As you can see, there are quite a few things MySQLi should learn from PDO if it wants to stay relevant. If it does, then there should really be no difference at all. People always talk about how you'd have to learn a whole new extension, yet they're actually already nearly identical for the most part. For this article, I'll be using native prepared statements, but emulated ones are perfectly acceptable as well. Here's a short writeup on the differences between the two. MySQLi AdvantagesPDO is also missing some features, albeit far less important ones for most users, such as:
So Which Should I Use?My opinion is that PDO should be used by default, especially beginners, due to its versatility, general predictability and useful fetch modes. However, MySQLi would be a better choice for advanced users who want the newest, MySQL-specific functionality. It's somewhat ironic that more experienced PHP developers tend think PDO is the only acceptable option 100% of the time, while beginners tend to use MySQLi. This is absolutely nutty from both ends. Of course most developers don't really need the extra advanced features MySQLi offers, but it certainly could be extremely useful for some, as previously mentioned. It's especially curious that novices are scared to try something "new" and switch to PDO, while a lot of advanced users recite the good ole "ease of switching from database driver" argument as PDO's advantage. Anyone who believes the myth that you can easily switch among databases in PDO seamlessly has obviously never attempted to do so. Each driver is different, and a switch from Microsoft SQL Server to MySQL will certainly not be automated. First of all let's make one thing clear, the syntax is very similar — almost identical, and I will present that in examples. PDO is also not some abstraction layer over MySQLi, but rather over PDO_MYSQL. If PDO does end up keeping up with all of the latest or distinct MySQL functionality, then I could see why MySQLi should go away; I'd even encourage it, if it ends up being the case. Though PDO does have several driver-specific features, it doesn't have all nor keep up with the latest. This is precisely why I don't think MySQLi and PDO aren't necessarily competitors, but rather two powerful libraries with completely different focuses for now. Obviously PDO should be more widely used, however. But as said before, the difference is pretty much negligible as is anyway. As mentioned several times earlier, MySQLi's survival relies on it catching up to PDO, along with PDO primarily sticking with features that are used among most of the DB drivers it supports. Sự khác biệt về mãNhư đã nêu trước đó, cả PDO và MySQLI đều cực kỳ giống nhau, nhưng có sự khác biệt nhỏ về cú pháp. MySQLI tuân theo quy ước Php Snake_case trường học cũ, trong khi PDO sử dụng Camelcase. Ngoài ra, các phương thức của MySQLI được sử dụng làm thuộc tính đối tượng, trong khi PDO sử dụng cú pháp truyền thống cho các chức năng. Tôi sẽ không bao giờ hiểu tại sao cả PDO và MySQLI đều phức tạp bằng cách buộc bạn sử dụng hai phương pháp riêng biệt để sử dụng câu lệnh đã chuẩn bị. May mắn thay, PDO đã loại bỏ nhu cầu sử dụng chức năng ràng buộc chuyên dụng - mặc dù tôi không chắc tại sao điều tương tự không được thực hiện cho 7. MySQLI và PDO không chuẩn bị thực sự không quá tệ, và đó thực sự là việc thực hiện không may các tuyên bố đã chuẩn bị khiến chúng có vẻ dài dòng. Chẳng hạn, trong API PostgreSQL dành riêng cho nhà cung cấp, bạn có thể làm điều đó như thế này. Dưới đây là một ví dụ về cách bạn thực hiện truy vấn "không chuẩn bị" để lấy mảng kết hợp với MySQLI và PDO, để tham khảo.
Trong thực tế, tuyến đường tốt nhất là sử dụng trình bao bọc, trình tạo truy vấn hoặc ORM. Tôi có một trình bao bọc Mysqli khá thô sơ mà bạn có thể thích. Mặc dù PDO là một bước đi đúng hướng, vì bạn có thể liên kết các giá trị trực tiếp vào thực thi, nhưng nó vẫn không lý tưởng. Trong lớp tôi đã thực hiện, bạn có thể xâu chuỗi tất cả các cuộc gọi của mình, trong khi chuyển các giá trị để liên kết dưới dạng đối số tham số. Kiểm tra những gì bạn có thể làm.
Bây giờ bạn có toàn bộ một mảng kết hợp được lưu trữ trong biến một cách ngắn gọn hơn nhiều. Thật lạ khi cả PDO và MySQLI không làm như vậy. Đối với phần còn lại của hướng dẫn, chúng tôi sẽ sử dụng các câu lệnh đã chuẩn bị, vì không có lý do chính đáng để không sử dụng chúng để bảo vệ tiêm SQL, trừ khi bạn sử dụng một tính năng như Async, hiện không hỗ trợ chúng. Nếu không, bạn sẽ cần phải định dạng thủ công các truy vấn của mình. Tạo kết nối cơ sở dữ liệu mớiPDO
Mysqli
Chèn, cập nhật, xóaPDO
Mysqli
Chèn, cập nhật, xóa Cần lưu ý rằng với PDO, bạn có thể chuỗi mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { $mysqli = new mysqli("localhost", "username", "password", "databaseName"); $mysqli->set_charset("utf8mb4"); } catch(Exception $e) { error_log($e->getMessage()); exit('Error connecting to database'); //Should be a message a typical user could understand } 8 và mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { $mysqli = new mysqli("localhost", "username", "password", "databaseName"); $mysqli->set_charset("utf8mb4"); } catch(Exception $e) { error_log($e->getMessage()); exit('Error connecting to database'); //Should be a message a typical user could understand } 7, mặc dù bạn sẽ không nhận được các hàng bị ảnh hưởng, vì vậy tôi không thể thấy điều đó hữu ích.PDO
Mysqli
Chèn, cập nhật, xóaCần lưu ý rằng với PDO, bạn có thể chuỗi 8 và 7, mặc dù bạn sẽ không nhận được các hàng bị ảnh hưởng, vì vậy tôi không thể thấy điều đó hữu ích.PDO
Mysqli 0Chèn, cập nhật, xóaPDO Mysqli 1Mysqli 2Chèn, cập nhật, xóa 3Cần lưu ý rằng với PDO, bạn có thể chuỗi 8 và 7, mặc dù bạn sẽ không nhận được các hàng bị ảnh hưởng, vì vậy tôi không thể thấy điều đó hữu ích. 4Nhận số lượng hàng bị ảnh hưởng 5Nhận khóa chính mới nhất được chènLưu ý, làm thế nào cả hai sử dụng biến kết nối, thay vì $stmt = $pdo->prepare("INSERT INTO myTable (name, age) VALUES (?, ?)"); $stmt->execute([$_POST['name'], 29]); $stmt = null; 0.PDO 6Mysqli 7Chèn, cập nhật, xóaPDO 8Mysqli 9Chèn, cập nhật, xóaPDO 0Mysqli 1Chèn, cập nhật, xóaPDO 2Mysqli 3Chèn, cập nhật, xóa Cần lưu ý rằng với PDO, bạn có thể chuỗi mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { $mysqli = new mysqli("localhost", "username", "password", "databaseName"); $mysqli->set_charset("utf8mb4"); } catch(Exception $e) { error_log($e->getMessage()); exit('Error connecting to database'); //Should be a message a typical user could understand } 8 và mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { $mysqli = new mysqli("localhost", "username", "password", "databaseName"); $mysqli->set_charset("utf8mb4"); } catch(Exception $e) { error_log($e->getMessage()); exit('Error connecting to database'); //Should be a message a typical user could understand } 7, mặc dù bạn sẽ không nhận được các hàng bị ảnh hưởng, vì vậy tôi không thể thấy điều đó hữu ích.PDO 4Mysqli 5Chèn, cập nhật, xóaCần lưu ý rằng với PDO, bạn có thể chuỗi 8 và 7, mặc dù bạn sẽ không nhận được các hàng bị ảnh hưởng, vì vậy tôi không thể thấy điều đó hữu ích.Nhận số lượng hàng bị ảnh hưởngPDO 6Mysqli 7Output: 8Chèn, cập nhật, xóaPDO 9Mysqli 0Output: 1Chèn, cập nhật, xóaPDO 2Mysqli 3Output: 4Chèn, cập nhật, xóaPDO 5Mysqli 6Output: 7Chèn, cập nhật, xóaPDO 8Mysqli 9Chèn, cập nhật, xóaPDO 0Mysqli 1Chèn, cập nhật, xóaPDO 2Mysqli Mysqli 3Chèn, cập nhật, xóa Các tham số được đặt tênĐây là một tính năng chỉ có PDO, nhưng nó chỉ hữu ích nếu chế độ mô phỏng bị tắt. Nếu không, bạn chỉ có thể sử dụng cùng một biến một lần. Cũng cần lưu ý rằng không bắt buộc đại tràng hàng đầu, nhưng điều này không được ghi nhận ở bất cứ đâu. Nó có thể cuối cùng sẽ dù sao, nhưng bạn không bao giờ biết, tôi cho rằng. Bạn cũng không thể trộn 9 người giữ chỗ với những người được đặt tên; Nó là cái này hay cái kia. 4
Tôi có thể sử dụng cả mysql và mysqli không?Có thể bao gồm cả MySQL và MySQLI khi kết nối với một cơ sở dữ liệu duy nhất, nhưng nó cực kỳ tinh tế và với một lượng lớn dữ liệu được truyền qua, nó có thể rất lộn xộn và khó kiểm soát. Nói chung, tốt nhất là sử dụng MySQLI bởi vì nó an toàn hơn và cập nhật hơn nhiều., but it is incredibly delicate and with large amounts of data being passed through it can get very messy and hard to control. it is best to use MySQLi in general in my opinion because it is much more secure and up to date.
Tốt hơn là sử dụng PDO hoặc MySQLI?Ưu điểm chính của PDO so với MySQLI là trong hỗ trợ cơ sở dữ liệu.PDO hỗ trợ 12 loại cơ sở dữ liệu khác nhau, đối lập với MySQLI, chỉ hỗ trợ MySQL.Khi bạn phải chuyển đổi dự án của mình để sử dụng cơ sở dữ liệu khác, PDO sẽ làm cho quy trình đơn giản hơn.. PDO supports 12 different database types, in opposition to MySQLi, which supports MySQL only. When you have to switch your project to use another database, PDO makes the process simpler.
PDO có nhanh hơn mysqli không?Về cơ bản, họ cho thấy rằng đối với các truy vấn được chọn sử dụng các câu lệnh đã chuẩn bị MySQLI chạy nhanh hơn một chút.Tuy nhiên, nó có thể không đáng kể tùy thuộc vào mục đích của bạn.Hãy nhớ rằng PDO theo mặc định sử dụng mô tả các câu lệnh được chuẩn bị phía máy khách.MySQLi runs a bit faster. Still it may not be significant depending on your purposes. Keep in mind that PDO by default uses client side prepared statements emulation.
Mysqli có phải là PDO không?Như đã nêu trước đó, cả PDO và MySQLI đều cực kỳ giống nhau, nhưng có sự khác biệt nhỏ về cú pháp.MySQLI tuân theo quy ước Php Snake_case trường học cũ, trong khi PDO sử dụng Camelcase.Ngoài ra, các phương thức của MySQLI được sử dụng làm thuộc tính đối tượng, trong khi PDO sử dụng cú pháp truyền thống cho các chức năng.both PDO and MySQLi are extremely similar, but there's slight differences in syntax. MySQLi follows the old-school PHP snake_case convention, while PDO uses camelCase. Additionally, MySQLi's methods are used as object properties, while PDO uses the traditional syntax for functions. |