Php+mysqli _php techniques for updating database instances using an object-oriented approach

Source: Internet
Author: User
Tags php programming

The example of this article tells the Php+mysqli method of using object-oriented updating database, and shares it for everyone's reference. The implementation methods are as follows:

Copy Code code as follows:
<?php
First step: Create a data Connection object
$mysqli = new Mysqli ("localhost", "root", "123456");/default MySQL class, its properties and methods see Manual
if ($mysqli->connect_error) {//connect_error as property, error
Die ("Database connection failed:". $mysqli->connect_errno. " --". $mysqli->connect_error);//Connect_errno: Error number
}
$mysqli->select_db ("Liuyan");//Select Database
$mysqli->query ("Set names ' GBK '");

Step two: Update one of the information, the following code
$sql = "Update news set title= ' Swimming World Cup leaf poetry title ' Where Id=17";
Step Three: Implement
$res = $mysqli->query ($sql);//Unlike the return value of the Select, the return value of update is true (if successful), and the failure returns false
Determine if execution is successful
if (! $res) {
echo "Failed to update data";
}else{
if ($mysqli->affected_rows>0) {//$mysql->affected_rows: Returns the number of database rows affected by the previous operation
echo "Update data Success";
}else{
echo "executed successfully, but no data update";//For example, when you execute this code for the second time, there is no data update
}
}
Close the database connection, unlike Select, where the query result set is not freed
$mysqli->close ();
?>

I hope this article will help you with your 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.