1: <?php namespace Laravel\Database\Connectors; use PDO;
2:
3: class MySQL extends Connector {
4:
5: /**
6: * Establish a PDO database connection.
7: *
8: * @param array $config
9: * @return PDO
10: */
11: public function connect($config)
12: {
13: extract($config);
14:
15: $dsn = "mysql:host={$host};dbname={$database}";
16:
17: // The developer has the freedom of specifying a port for the MySQL database
18: // or the default port (3306) will be used to make the connection by PDO.
19: // The Unix socket may also be specified if necessary.
20: if (isset($config['port']))
21: {
22: $dsn .= ";port={$config['port']}";
23: }
24:
25: // The UNIX socket option allows the developer to indicate that the MySQL
26: // instance must be connected to via a given socket. We'll just append
27: // it to the DSN connection string if it is present.
28: if (isset($config['unix_socket']))
29: {
30: $dsn .= ";unix_socket={$config['unix_socket']}";
31: }
32:
33: $connection = new PDO($dsn, $username, $password, $this->options($config));
34:
35: // If a character set has been specified, we'll execute a query against
36: // the database to set the correct character set. By default, this is
37: // set to UTF-8 which should be fine for most scenarios.
38: if (isset($config['charset']))
39: {
40: $connection->prepare("SET NAMES '{$config['charset']}'")->execute();
41: }
42:
43: return $connection;
44: }
45:
46: }