php+mysqli uses object-oriented method to update database instance, mysqli object-oriented_PHP tutorial

WBOY
Release: 2016-07-13 10:08:55
Original
1331 people have browsed it

php+mysqli uses object-oriented approach to update database instances, mysqli is object-oriented

The example in this article describes how php+mysqli uses the object-oriented method to update the database, and is shared with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
//Step one: Create data connection object
$mysqli = new MySQLi("localhost","root","123456");//Default MySQL class, its properties and methods can be found in the manual
if($mysqli->connect_error){//connect_error is an attribute and an error is reported
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 2: Update one of the information, the code is as follows
$sql = "update news set title='Ye Shiwen wins swimming World Cup' where id=17";
//Step 3: Execute
$res = $mysqli->query($sql);//Unlike the return value of select, the return value of update is true (if successful), otherwise it returns false
//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 database rows affected by the previous operation
echo "Data updated successfully";
}else{
echo "Execution successful, but no data updated";//For example, when you execute this code for the second time, there will be no data update
}
}
//Close the database connection. Unlike select, there is no need to release the query result set
$mysqli->close();
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/949454.htmlTechArticlephp+mysqli uses object-oriented method to update database instances, mysqli is object-oriented. This article describes the use of object-oriented method by php+mysqli. How to update the database, share it with everyone for everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!