mysql.php 1.1 KB

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