This article introduces to you the code segments that are commonly used in the actual development of practical PHP websites to operate the MySQL database, all The code is executed reliably, and this article will be continuously updated! ! !
1. Insert the data table into the database
2. Insert new records into the database table
3. Read all the contents of the data table
";//输出表中所有openid字段的值 } mysql_close($con);//关闭mysql连接 ?>
4 , Read matching data from the data table
";//输出表中所有openid字段的值 } mysql_close($con);//关闭mysql连接 ?>
5. Modify the data in the database table
6. Delete records from the data table
7. Delete from the database Data table
PHP Data Object (PDO) extension defines a lightweight consistent interface for PHP to access the database. Provides a data access abstraction layer, which means that no matter which database is used, the same functions (methods) can be used to query and obtain data.
PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0. It cannot run on previous PHP versions.
The following is an example to illustrate the usage of PDO:
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //设置 PDO 错误模式,用于抛出异常 $sql = "CREATE TABLE abc ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, openid varchar(32) NOT NULL, nickname varchar(32) NOT NULL, sex varchar(8) NOT NULL )";//创建名称为abc的数据表,id不能为空且自动递增并设置为主键 $conn->exec($sql);//使用exec()没有结果返回 } catch(PDOException $e){ echo $sql . "
" . $e->getMessage();//显示异常信息 } $conn = null;//关闭连接 ?>
If the environment permits, use PDO for MySQL database operations as much as possible.