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.