Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SQL][PHP] Spawdzenie kodu
julek12
post
Post #1





Grupa: Zarejestrowani
Postów: 97
Pomógł: 5
Dołączył: 6.02.2009
Skąd: Gorzów Wlkp.

Ostrzeżenie: (0%)
-----


Witam,
Czy mógłby ktoś przejrzeć kod i wytknąć mi moje błędy?

  1. <?php
  2. /**
  3.  * TibiaServ
  4.  * Website: http://tibiaserv.pl
  5.  *
  6.  * @author Juliusz Marciniak <juliusz.marciniak@gmail.com>
  7.  * @date 20.10.2009
  8.  * @version 0.1
  9.  * @copyright Copyright (c) 2009 Juliusz Marciniak, All Rights Reserved
  10.  */
  11.  
  12. class Pdo_Database_Engine
  13. {
  14. /**
  15. * Connection to the database.
  16. *
  17. * @access public
  18. * @param string The database DSN.
  19. * @param string The database username. (depends on DSN)
  20. * @param string The database user's password. (depends on DSN)
  21. * @param array The databases driver options (optional)
  22. * @return boolean True on success
  23. */
  24. public function __construct($db_dsn, $db_login = '', $db_password = '', $db_driver_options = array())
  25. {
  26. try
  27. {
  28. $this -> pdo = new PDO($db_dsn, $db_login, $db_password, $db_driver_options);
  29. }
  30. catch(PDOException $exception)
  31. {
  32. die('Connection failed: <br /><b>' . $exception -> getMessage() . '</b>');
  33. }
  34. $this -> pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35.  
  36. return true;
  37. }
  38.  
  39. /**
  40. * Performs a simple select query.
  41. *
  42. * @access public
  43. * @param string The table name to be queried.
  44. * @param string List of fields to be selected.
  45. * @param string SQL formatted list of conditions to be matched.
  46. * @param array List of options: bind, order by, limit and fetch_array
  47. */
  48. public function query_select($table, $fields = '*', $conditions = '', $options = array())
  49. {
  50. $query = 'SELECT ' . $fields . ' FROM ' . $table;
  51.  
  52. if ($conditions != '')
  53. {
  54. $query .= ' WHERE ' . $conditions;
  55. }
  56.  
  57. if (isset($options['order_by']))
  58. {
  59. $query .= ' ORDER BY ' . $options['order_by'];
  60. }
  61.  
  62. if (isset($options['limit']))
  63. {
  64. $query .= ' LIMIT ' . $options['limit'];
  65. }
  66.  
  67. if (isset($options['bind']))
  68. {
  69. $bind = $options['bind'];
  70. $query = $this -> pdo -> prepare($query);
  71. foreach($bind as $parameter => $value)
  72. {
  73. if (is_array($value))
  74. {
  75. $query -> bindValue($parameter, $value[0], $value[1]);
  76. }
  77. else
  78. {
  79. $query -> bindValue($parameter, $value);
  80. }
  81. }
  82. $query -> execute();
  83. }
  84. else
  85. {
  86. $query = $this -> pdo -> query($query);
  87. }
  88.  
  89. if (isset($options['fetch_array']))
  90. {
  91. if (is_array($options['fetch_array']))
  92. {
  93. $fetch_array = $options['fetch_array'];
  94. return $this -> fetch_array($query, $fetch_array['fetch_style']);
  95. }
  96. else
  97. {
  98. return $this -> fetch_array($query);
  99. }
  100. }
  101. else
  102. {
  103. return $query;
  104. }
  105. }
  106.  
  107. public function fetch_array($query, $fetch_style = PDO::FETCH_BOTH)
  108. {
  109. if (!is_object($query))
  110. {
  111. return '<b>Warning: Invalid argument</b>';
  112. }
  113.  
  114. $fetch_array = $query -> fetch($fetch_style);
  115.  
  116. return $fetch_array;
  117. }
  118.  
  119. /**
  120. * The end of the connection to the database.
  121. *
  122. * @access public
  123. * @return boolean True on success
  124. */
  125. public function __destruct()
  126. {
  127. $this -> pdo = null;
  128.  
  129. return true;
  130. }
  131. }
  132. ?>


Później wykorzystuje to na przykład tak:
  1. $a = new Pdo_Database_Engine('mysql:dbname=test;host=localhost', 'xxx', 'xxx', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
  2.  
  3. $b = $a -> query_select('`bany`', '*', '`typ` = :typ AND `asdasd` = :asdasd', array('fetch_array' => array(true, 'fetch_style' => PDO::FETCH_ASSOC), 'bind' => array(':typ' => array(2, PDO::PARAM_INT), ':asdasd' => 2)));
  4. print_r($b);

Początek mam dobrze, bo patrzyłem z manuala, chodzi mi o te 2 metody.

Ten post edytował julek12 20.11.2009, 08:47:48
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 06:14