PHP+MYSQLI updating DB instances using object-oriented methods, mysqli object-oriented
This paper describes the method of php+mysqli using object-oriented approach to update the database, and share it for everyone's reference. The implementation method is as follows:
Copy the 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 is a 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 '");
The second step: update one of the information, the code is as follows
$sql = "Update news set title= ' Swimming World Cup leaf poetry title ' Where Id=17 ';
Step Three: Execute
$res = $mysqli->query ($sql);//Unlike the return value of SELECT, the return value of update is true (if successful), 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 "Successful execution, 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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/949454.html www.bkjia.com true http://www.bkjia.com/PHPjc/949454.html techarticle php+mysqli Updating a DB instance using object-oriented approach, MYSQLI object-oriented This article describes the Php+mysqli method of updating the database using object-oriented methods, share to everyone ...