Migration to PDO database abstraction layer
[mirrors/Kyberia-bloodline.git] / wwwroot / inc / database.inc
1 <?php
2
3 require("result.inc");
4
5 function db_escape_string($str) {
6 global $db;
7 //This function should be used in whole project instead of *_escape_string() functions!
8 //return mysql_escape_string($str); //XXX TODO $db->quote($str), mysql_real_escape_string() or pg_escape_string() should be used here!
9 return preg_replace('(^.|.$)', '', $db->quote($str)); //XXX HACK
10 }
11
12 class CLASS_DATABASE extends PDO {
13 //All functions in this class are deprecated!
14 //Please use only native PDO functions!
15
16 var $Master = true;
17 var $_linkId = false;
18 var $_url = "";
19 var $_user = "";
20 var $_password = "";
21 var $_database = "";
22 var $_halt_on_error = true;
23
24 function __construct() {
25 $this->connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
26 }
27
28 protected function connect($host, $user, $password, $database, $halt_on_error = true) {
29 global $error;
30 parent::__construct("mysql:host=$host;dbname=$database", $user,
31 $password);
32 /*{
33 $error='chcipla databaza';
34 $this->exception($error); //deprecated
35 }; */
36 $this->setAttribute(PDO::ATTR_STATEMENT_CLASS,
37 array('result', array($this)));
38
39 $this->_halt_on_error = $halt_on_error;
40 $this->_url = $host;
41 $this->_user = $user;
42 $this->_password = $password;
43 /* if ($this->_linkId == false) {
44 $this->_linkId=mysql_connect($host, $user, $password);
45 if ($this->_linkId == false) {
46 $error='chcipla databaza';
47 $this->exception($error);
48 return false;
49 //die();
50 }// else {
51 // mysql_query('set character set utf8');
52 //}
53 $this->_url=$host;
54 $this->_user=$user;
55 $this->_password=$password;
56
57 if ($this->_linkId == false || mysql_select_db($database, $this->_linkId) == false) {
58 $this->exception("1Database failed.");
59 return false;
60 die();
61 }
62 $this->_database=$database;
63 }
64 */
65 return true;
66 }
67
68 function update($sql) { //DEPRECATED!!! Use $db->query($sql)->rowCount(); instead!!!
69 if (!$this->Master) {
70 $this->_linkId = false;
71 $this->connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
72 $this->Master = true;
73 }
74
75 $this->_queryId = $this->query($sql);
76 if ($this->_queryId == false) {
77 $this->exception("update failed.");
78 }
79 $rows = @$this->_queryId->rowCount();
80 return ($rows);
81 }
82
83 function getLastInsertId() { //DEPRECATED!!! Use $db->lastInsertId(); instead!!!
84 return (@$this->lastInsertId());
85 }
86
87 protected function exception($errorMessage) {
88
89 echo "<!-- ";
90 //echo @mysql_error($this->_linkId)," (",@mysql_errno($this->_linkId),")";
91 echo "-->";
92
93 if ($this->_halt_on_error) {
94 die("<pre>".$errorMessage."</pre>");
95 } else {
96 echo $errorMessage."<br>";
97 return false;
98 }
99 }
100 }
101
This page took 0.296233 seconds and 4 git commands to generate.