mysql.php 1.28 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
		$dsn = "mysql:host={$host};dbname={$database}";
16

Taylor Otwell committed
17 18 19 20
		// 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.
		if (isset($config['port']))
21
		{
Taylor Otwell committed
22 23 24
			$dsn .= ";port={$config['port']}";
		}

Taylor Otwell committed
25 26 27
		// The UNIX socket option allows the developer to indicate that the MySQL
		// instance must be connected to via a given socket. We'll just append
		// it to the DSN connection string if it is present.
Taylor Otwell committed
28 29 30
		if (isset($config['unix_socket']))
		{
			$dsn .= ";unix_socket={$config['unix_socket']}";
31 32 33 34
		}

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

Taylor Otwell committed
35 36 37
		// 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.
38 39
		if (isset($config['charset']))
		{
40
			$connection->prepare("SET NAMES '{$config['charset']}'")->execute();
41 42
		}

43
		return $connection;
44 45 46
	}

}