1. sfMySQLDatabase.class.php
  2. /** * sfMySQLDatabase provides connectivity for the MySQL brand database. * * Optional parameters: * * # database - [none] - The database name. * # host - [localhost] - The database host. * # username - [none] - The database username. * # password - [none] - The database password. * # persistent - [No] - Indicates that the connection should be persistent. * * @package symfony * @subpackage database * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfMySQLDatabase.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfMySQLDatabase extends sfDatabase
  4. {
  5. /**
  6. * Connects to the database.
  7. *
  8. * @throws <b>sfDatabaseException</b> If a connection could not be created
  9. */
  10. public function connect()
  11. {
  12. $database = $this->getParameter('database');
  13. $host = $this->getParameter('host', 'localhost');
  14. $password = $this->getParameter('password');
  15. $username = $this->getParameter('username');
  16. $encoding = $this->getParameter('encoding');
  17. // let's see if we need a persistent connection
  18. $connect = $this->getConnectMethod($this->getParameter('persistent', false));
  19. if ($password == null)
  20. {
  21. if ($username == null)
  22. {
  23. $this->connection = @$connect($host);
  24. }
  25. else
  26. {
  27. $this->connection = @$connect($host, $username);
  28. }
  29. }
  30. else
  31. {
  32. $this->connection = @$connect($host, $username, $password);
  33. }
  34. // make sure the connection went through
  35. if ($this->connection === false)
  36. {
  37. // the connection's foobar'd
  38. throw new sfDatabaseException('Failed to create a MySQLDatabase connection.');
  39. }
  40. // select our database
  41. if ($this->selectDatabase($database))
  42. {
  43. // can't select the database
  44. throw new sfDatabaseException(sprintf('Failed to select MySQLDatabase "%s".', $database));
  45. }
  46. // set encoding if specified
  47. if ($encoding)
  48. {
  49. @mysql_query("SET NAMES '".$encoding."'", $this->connection);
  50. }
  51. // since we're not an abstraction layer, we copy the connection
  52. // to the resource
  53. $this->resource = $this->connection;
  54. }
  55. /**
  56. * Returns the appropriate connect method.
  57. *
  58. * @param bool $persistent wether persistent connections are use or not
  59. * @return string name of connect method.
  60. */
  61. protected function getConnectMethod($persistent)
  62. {
  63. return $persistent ? 'mysql_pconnect' : 'mysql_connect';
  64. }
  65. /**
  66. * Selects the database to be used in this connection
  67. *
  68. * @param string $database Name of database to be connected
  69. *
  70. * @return bool true if this was successful
  71. */
  72. protected function selectDatabase($database)
  73. {
  74. return ($database != null && !@mysql_select_db($database, $this->connection));
  75. }
  76. /**
  77. * Execute the shutdown procedure
  78. *
  79. * @return void
  80. *
  81. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
  82. */
  83. public function shutdown()
  84. {
  85. if ($this->connection != null)
  86. {
  87. @mysql_close($this->connection);
  88. }
  89. }
  90. }

Debug toolbar