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

2021/7/3 19:24:53

本文主要是介绍【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   



这篇关于【Laravel3.0.0源码阅读分析】MySQL连接类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程