mysql.php 1.06 KB
Newer Older
1 2 3 4 5
<?php namespace Laravel\Database\Connectors; use PDO;

class MySQL extends Connector {

	/**
6
	 * Establish a PDO database connection.
7 8 9 10 11 12
	 *
	 * @param  array  $config
	 * @return PDO
	 */
	public function connect($config)
	{
13
		extract($config);
14 15 16 17

		// Format the initial MySQL PDO connection string. These options are required
		// for every MySQL connection that is established. The connection strings
		// have the following convention: "mysql:host=hostname;dbname=database"
18
		$dsn = "mysql:host={$host};dbname={$database}";
19 20 21 22 23 24

		// Check for any optional MySQL PDO options. These options are not required
		// to establish a PDO connection; however, may be needed in certain server
		// or hosting environments used by the developer.
		foreach (array('port', 'unix_socket') as $key => $value)
		{
25 26 27 28 29 30 31 32 33 34 35
			if (isset($config[$key]))
			{
				$dsn .= ";{$key}={$value}";
			}
		}

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

		if (isset($config['charset']))
		{
			$connection->prepare("SET NAMES '{$charset}'")->execute();
36 37
		}

38
		return $connection;
39 40 41
	}

}