mysql.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // The UNIX socket option allows the developer to indicate that the MySQL
  21. // instance must be connected to via a given socket. We'll just append
  22. // it to the DSN connection string if it is present.
  23. if (isset($config['unix_socket']))
  24. {
  25. $dsn .= ";unix_socket={$config['unix_socket']}";
  26. }
  27. $connection = new PDO($dsn, $username, $password, $this->options($config));
  28. // If a character set has been specified, we'll execute a query against
  29. // the database to set the correct character set. By default, this is
  30. // set to UTF-8 which should be fine for most scenarios.
  31. if (isset($config['charset']))
  32. {
  33. $connection->prepare("SET NAMES '{$config['charset']}'")->execute();
  34. }
  35. return $connection;
  36. }
  37. }