Php + mysqli update database instances using object-oriented methods

Source: Internet
Author: User
This article describes how php + mysqli updates the database using object-oriented methods. The example analyzes the tips for creating, connecting, updating, and returning update results for mysqli objects, for more information about how to use php + mysqli to update databases using object-oriented methods, see the following example. The specific implementation method is as follows:

The code is as follows:

<? Php
// Step 1: Create a data connection object
$ Mysqli = new MySQLi ("localhost", "root", "123456"); // Default MySQL class. for its attributes and methods, see the manual.
If ($ mysqli-> connect_error) {// connect_error is the attribute and an error is returned.
Die ("database connection failed:". $ mysqli-> connect_errno. "--". $ mysqli-> connect_error); // connect_errno: Error No.
}
$ Mysqli-> select_db ("liuyan"); // select a database
$ Mysqli-> query ("set names 'gbk '");

// Step 2: update one of the information. the code is as follows:
$ SQL = "update news set title = 'swimming World Cup winner Ye Shiwen 'Where id = 17 ";
// Step 3: Execute
$ Res = $ mysqli-> query ($ SQL); // unlike the select return value, the update return value is true (if successful), and false is returned if failure.
// Determine whether the execution is successful
If (! $ Res ){
Echo "failed to update data ";
} Else {
If ($ mysqli-> affected_rows> 0) {// $ mysql-> affected_rows: returns the number of rows affected by the previous operation
Echo "data updated ";
} Else {
Echo "execution succeeded, but no data update"; // for example, when you execute this code for the second time, there will be no data update
}
}
// Close the database connection. Unlike select, you do not need to release the query result set here.
$ Mysqli-> close ();
?>

I hope this article will help you with php programming.

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.