PHP uses the mysqli extension library to add, delete, modify, and query (object-oriented version), and mysqli object-oriented

Source: Internet
Author: User

PHP uses the mysqli extension library to add, delete, modify, and query (object-oriented version), and mysqli object-oriented

The mysqli extension library is an improved version of the mysql extension library. It improves stability and efficiency based on the mysql extension library. The mysqli extension library has two sets of things, one is process-oriented mysqli, and the other is object-oriented mysqli. The operation method is basically the same as that of the mysql extension library. This time, we will first extract a tool class to operate mysql and the called class.

1. mysqli extended library operation database tool class

<? Php // database operation class DBUtil {private $ host = "localhost"; private $ username = "root"; private $ password = "123456"; private $ dbname = "student "; private $ conn; public function DBUtil () {$ this-> conn = new mysqli ($ this-> host, $ this-> username, $ this-> password, $ this-> dbname) or die ($ this-> conn-> connect_error);} // query public function query ($ SQL) {$ all = $ this-> conn-> query ($ SQL); return $ all;} // insert, modify, and delete public func Tion otherOperate ($ SQL) {if ($ this-> conn-> query ($ SQL) {if ($ this-> conn-> affected_rows> 0) {return "OK" ;}else {return "ERROOR" ;}} public function close () {$ this-> conn-> close () ;}}?>

2. The following is the code for calling the tool class.

<? Php require_once "MySQLUtil. php ";/* $ SQL =" select * from m_student "; $ util = new DBUtil (); $ result = $ util-> query ($ SQL ); while ($ row = $ result-> fetch_assoc () {echo "$ row [stuName]". "</br>" ;}$ result-> free (); $ util-> close (); */$ SQL = "update m_student set stuName = 'yang mi 'where id = 3"; $ util = new DBUtil (); $ result = $ util-> otherOperate ($ SQL); echo $ result; $ util-> close ();?>

Read more: www.manongjc.com/article/1206.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.