Home>Article>Backend Development> 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
autoload Original
2021-03-05 10:18:13 2571browse

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?PDOis this master key, which can open any database door.

Definition:

PDOis the abbreviation ofPHP Data Object, which represents PHP data object and is a pureObject-orientedDatabase operation extensionimplemented 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

    PDOStatementclass 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

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 usingconstantsinside PDO. (This can be omitted)

3. Data type after PDO instantiates the object

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!

Statement:
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