PHP
We often need to connect to the database to add, delete, modify, and check data. Previously, we generally used mysqli## to operate the database. # connection method, but as the advantages of
PDO gradually emerge, the connection method of
PDO has become mainstream. This article will take you to take a look and use
PDOThe
Statement object returned after connection.
1. Database used
CREATE TABLE `fate` ( `id` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `fate` VALUES ('1', 'saber', '100'), ('2', 'acher', '77'), ('3', 'luncher', '56'), ('4', 'cooker', '18')
2. Obtain Statement object:
<?php $servername = "localhost";//数据库主机名 $username = "root";//用户名 $password = "root123456";//密码 $dbname = "my_database";//数据库名称 try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); echo "连接成功"."<br>"; $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "select * from fate"; $statement = $conn->query($sql); } catch(PDOException $e) { echo $e->getMessage(); } ?>
3. Statement’s more commonly used attributes
《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of Parsing PDO::Statement in PHP. For more information, please follow other related articles on the PHP Chinese website!