创建 PDO 实例请求连接SQL数据库

PDO :: __ construct —创建一个PDO实例,表示与数据库的连接。(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)

描述:

public PDO::__construct ( string $dsn [, string $username [, string $passwd [, array $options ]]] )

支持的数据库:

  • MySQL,MariaDB:mysql:host=localhost;dbname=mydatabase
  • SQLite:sqlite:/path/to/database/file
  • PostgreSQL(8.4或更高版本):pgsql:host=localhost;port=5432;dbname=mydatabase
  • CUBRID(9.3或更高版本):cubrid:dbname=demodb;host=localhost;port=33000
  • MS SQL Server(2008或更高版本,通过sqlsrv驱动程序):sqlsrv:Server=localhost;Database=mydatabase
  • MS SQL Server(2008或更高版本,通过dblib驱动程序):dblib:host=localhost;dbname=mydatabase
  • MS SQL Server(2008或更高版本,通过mssql驱动程序):mssql:host=localhost;dbname=mydatabase
  • Oracle:oci:dbname=//localhost:1521/mydatabase

连接示例:

/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:host=localhost;dbname=test';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

相关文章:

1、PDO数据抽象简介及MySQL,PostgreSQL,ODBC,Oracle应用
https://www.onexin.net/database/pdo-data-abstraction-introduction-and-mysql-postgresql-odbc-oracle-applications/

2、常用MySQL的命令集锦
https://www.onexin.net/database/sql-design/used-the-mysql-command-collection/

3、MYSQL出错代码列表大全(中文对照)
https://www.onexin.net/database/mysql-error-code-list-daquan-chinese-control/

4、MySQL中my.cnf配置文件及参数介绍
https://www.onexin.net/database/mysql-my-cnf-configuration-files-and-parameters-introduced/

5、MySQL数thread_cache和table_cache详解
https://www.onexin.net/database/the-mysql-the-number-thread_cache-and-table_cache-xiangjie/

转载请注明出处:https://www.onexin.net/latest/pdo-database/

Leave a Reply

Your email address will not be published. Required fields are marked *