TAILIEUCHUNG - Object-Oriented Programming with PHP 5 phần 8

Chúng tôi sẽ xem xét một số các tính năng này trong các ví dụ sau đây. Nhưng tất nhiên, chúng tôi sẽ không cho bất cứ điều gì giới thiệu cho MySQL, bởi vì đó là ra khỏi phạm vi cuốn sách này. Chúng tôi sẽ chỉ cho bạn thấy làm thế nào để sử dụng giao diện hướng đối tượng bằng cách sử dụng MySQLi . | Database in an OOP Way Ability to create compressed connections Ability to connect over SSL Support for Prepared Statements Support for Stored Procedure SP Support for better replication and transaction We will look into some of these features in the following examples. But of course we are not going for anything introductory to MySQL because that is out of scope for this book. We will just show you how to use OO interface using MySQLi and how to use some of these advanced features. Connecting to MySQL in an OO Way Remember those old days when you had to use procedural function call to connect to MySQL even from your objects. Those days are over. Now you can take advantage of complete OO interface of MySQLi to talk to MySQL well there are a few procedural methods but overall it s completely OO . Take a look at the following example mysqli new mysqli localhost user password dbname if mysqli_connect_errno echo Failed to connect the error message is . mysqli_connect_error exit If the connection fails you may get an error message like this Failed to connect the error message is Access denied for user my_user @ localhost using password YES Selecting Data in an OO Way Let s see how to select data from a table in an OO way using MySQLi API. php mysqli new mysqli localhost un pwd db if mysqli_connect_errno echo Failed to connect the error message is . mysqli_connect_error exit 170 Chapter 7 close connection result mysqli - query select from users while data result- fetch_object echo data- name. . data- pass. n The output is as following robin no password tipu bolajabena Please note that it is not good practice to store users passwords in plain text in your database without encrypting them in some way. The best way is to store just the hash of their passwords using some hash routines like md5 Updating Data in an OO Way There is no special deal with it. You can update your data as you previously did with MySQL extension. But for the sake of OO style we are showing an example

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.