Home > Backend Development > PHP Tutorial > Open up the two channels of Ren and Du to realize the connection between mysql and php

Open up the two channels of Ren and Du to realize the connection between mysql and php

autoload
Release: 2023-04-09 19:34:01
Original
2686 people have browsed it

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-orientedDatabase operation extension implemented in an object-oriented manner.

1. Although the PDO class provides many methods, the commonly used methods are as follows:

  • PDO::__construct(): Instantiate a PDO object

  • PDO::exec(): Execute a write operation SQL command and return the number of affected rows

  • PDO: :query(): Execute a read operation SQL command and return a

    PDOStatement class object (the latter performs data parsing operations)

  • PDO::errorCode() and PDO::errorInfo(): Get the last error information (error code and error description array)

2. PDO instantiation object

<?php
   //方案1:直接写入数据进行数据库初始化
   $pdo = new PDO(&#39;mysql:host=localhost;port=3306;dbname=my_database&#39;,&#39;root&#39;,&#39;root&#39;);
   //方案2:利用变量保存数据来实现数据库初始化(数据来源可以是其他配置文件:安全)
   $dsn = &#39;mysql:host=localhost;dbname=my_database&#39;;
   $user = &#39;root&#39;;
   $pass = &#39;root&#39;;
   $pdo = new PDO($dsn,$user,$pass);
?>
Copy after login

The instantiated object uses its construction method __construct(string $dsn, string $user, string $pass[, array $drivers]) to implement

  1. $dsn: basic information of a database String, including database product, host address, etc.

    Format: mysql:host=localhost;port=3306;dbname=my_database

    Mysql represents the database type

    Host represents the

    host address

               port represents the

    port number (the default port number 3306 can be omitted)

              dbname represents the database name

  2. $user: user name, if the database allows anonymous users to appear, then there is no need for this parameter (only $dsn).

  3. $pass: Password, the same as the username.

  4. $drivers: PDO attribute settings, which are

    associative arrays, set using constants inside PDO. (This can be omitted)

3. Data type after PDO instantiates the object

<?php
     $pdo = new PDO(&#39;mysql:host=localhost;port=3306;dbname=my_database&#39;,&#39;root&#39;,&#39;root&#39;);
     
     var_dump($pdo);//object(PDO)#1 (0) { }
?>
Copy after login

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!

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