数据库
首页 > 数据库> > 【Laravel3.0.0源码阅读分析】MySQL连接类

【Laravel3.0.0源码阅读分析】MySQL连接类

作者:互联网

<?php namespace Laravel\Database\Connectors; use PDO;

class MySQL extends Connector {

	/**
	 * Establish a PDO database connection.
	 * 建立 PDO 数据库连接。
	 * @param  array  $config
	 * @return PDO
	 */
	public function connect($config)
	{
		extract($config);

		$dsn = "mysql:host={$host};dbname={$database}";

		// The developer has the freedom of specifying a port for the MySQL database
		// or the default port (3306) will be used to make the connection by PDO.
		// The Unix socket may also be specified if necessary.
        // 开发者可以自由指定 MySQL 数据库的端口,或者默认端口(3306)将用于通过 PDO 进行连接。如果需要,也可以指定 Unix 套接字。
		if (isset($config['port']))
		{
			$dsn .= ";port={$config['port']}";
		}

		if (isset($config['unix_socket']))
		{
			$dsn .= ";unix_socket={$config['unix_socket']}";
		}

		$connection = new PDO($dsn, $username, $password, $this->options($config));

		// If a character set has been specified, we'll execute a query against
		// the database to set the correct character set. By default, this is
		// set to UTF-8 which should be fine for most scenarios.
        // 如果已指定字符集,我们将对数据库执行查询以设置正确的字符集。 默认情况下,它被设置为 UTF-8,这对于大多数情况应该没问题。
		if (isset($config['charset']))
		{
			$connection->prepare("SET NAMES '{$config['charset']}'")->execute();
		}

		return $connection;
	}

}

github地址: https://github.com/liu-shilong/laravel3-scr   

标签:execute,set,UTF,charset,character,Laravel3.0,源码,MySQL,config
来源: https://blog.51cto.com/u_14097531/2972963