Home > Article > Backend Development > Open up the two channels of Ren and Du to realize the connection between mysql and php
If you want to cook a good dish, you must first have unusual cooking skills, and secondly, have fresh ingredients, and these fresh ingredients are lying in this database. How to open the door of this database, a master key It's enough, why need more? PDO
is this master key, which can open any database door.
Definition:
PDO
is the abbreviation of PHP Data Object
, which represents PHP data object and is a pure Object-oriented
Database operation extension implemented in an object-oriented manner.
1. Although the PDO class provides many methods, the commonly used methods are as follows:
PDOStatement class object (the latter performs data parsing operations)
2. PDO instantiation object
<?php //方案1:直接写入数据进行数据库初始化 $pdo = new PDO('mysql:host=localhost;port=3306;dbname=my_database','root','root'); //方案2:利用变量保存数据来实现数据库初始化(数据来源可以是其他配置文件:安全) $dsn = 'mysql:host=localhost;dbname=my_database'; $user = 'root'; $pass = 'root'; $pdo = new PDO($dsn,$user,$pass); ?>The instantiated object uses its construction method __construct(string $dsn, string $user, string $pass[, array $drivers]) to implement
host address
port number (the default port number 3306 can be omitted)
associative arrays, set using
constants inside PDO. (This can be omitted)
3. Data type after PDO instantiates the object
<?php $pdo = new PDO('mysql:host=localhost;port=3306;dbname=my_database','root','root'); var_dump($pdo);//object(PDO)#1 (0) { } ?>Recommended:
php tutorial ,php video tutorial
The above is the detailed content of Open up the two channels of Ren and Du to realize the connection between mysql and php. For more information, please follow other related articles on the PHP Chinese website!