summaryrefslogtreecommitdiff
path: root/includes/mysql_provider.php
blob: 9f901a40665a5d5860dc998f47a7c56a061b17ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php

/**
 * Close connection.
 */
function sql_close() {
  global $sql_connection;

  return $sql_connection->close();
}

/**
 * Start new transaction.
 */
function sql_transaction_start() {
  global $sql_nested_transaction_level;

  if ($sql_nested_transaction_level ++ == 0)
    return sql_query("BEGIN");
  else
    return true;
}

/**
 * Commit transaction.
 */
function sql_transaction_commit() {
  global $sql_nested_transaction_level;

  if (-- $sql_nested_transaction_level == 0)
    return sql_query("COMMIT");
  else
    return true;
}

/**
 * Stop transaction, revert database.
 */
function sql_transaction_rollback() {
  global $sql_nested_transaction_level;

  if (-- $sql_nested_transaction_level == 0)
    return sql_query("ROLLBACK");
  else
    return true;
}

/**
 * Logs an sql error.
 *
 * @param string $message
 * @return false
 */
function sql_error($message) {
  sql_close();

  $message = trim($message) . "\n";
  $message .= debug_string_backtrace() . "\n";

  error_log('mysql_provider error: ' . $message);

  return false;
}

/**
 * Connect to mysql server.
 *
 * @param string $host
 *          Host
 * @param string $user
 *          Username
 * @param string $pass
 *          Password
 * @param string $db
 *          DB to select
 * @return mysqli The connection handler
 */
function sql_connect($host, $user, $pass, $db) {
  global $sql_connection;

  $sql_connection = new mysqli($host, $user, $pass, $db);
  if ($sql_connection->connect_errno)
    return sql_error("Unable to connect to MySQL: " . $sql_connection->connect_error);

  $result = $sql_connection->query("SET CHARACTER SET utf8;");
  if (! $result)
    return sql_error("Unable to set utf8 character set (" . $sql_connection->errno . ") " . $sql_connection->error);

  $result = $sql_connection->set_charset('utf8');
  if (! $result)
    return sql_error("Unable to set utf8 names (" . $sql_connection->errno . ") " . $sql_connection->error);

  return $sql_connection;
}

/**
 * Change the selected db in current mysql-connection.
 *
 * @param
 *          $db_name
 * @return bool true on success, false on error
 */
function sql_select_db($db_name) {
  global $sql_connection;
  if (! $sql_connection->select_db($db_name))
    return sql_error("No database selected.");
  return true;
}

/**
 * MySQL SELECT query
 *
 * @param string $query
 * @return Result array or false on error
 */
function sql_select($query) {
  global $sql_connection;

  $result = $sql_connection->query($query);
  if ($result) {
    $data = array();
    while ($line = $result->fetch_assoc())
      array_push($data, $line);
    return $data;
  } else
    return sql_error("MySQL-query error: " . $query . " (" . $sql_connection->errno . ") " . $sql_connection->error);
}

/**
 * MySQL execute a query
 *
 * @param string $query
 * @return mysqli_result boolean resource or false on error
 */
function sql_query($query) {
  global $sql_connection;

  $result = $sql_connection->query($query);
  if ($result) {
    return $result;
  } else
    return sql_error("MySQL-query error: " . $query . " (" . $sql_connection->errno . ") " . $sql_connection->error);
}

/**
 * Returns last inserted id.
 *
 * @return int
 */
function sql_id() {
  global $sql_connection;
  return $sql_connection->insert_id;
}

/**
 * Escape a string for a sql query.
 *
 * @param string $query
 * @return string
 */
function sql_escape($query) {
  global $sql_connection;
  return $sql_connection->real_escape_string($query);
}

/**
 * Count query result lines.
 *
 * @param string $query
 * @return int Count of result lines
 */
function sql_num_query($query) {
  global $sql_connection;
  return sql_query($query)->num_rows;
}

function sql_select_single_col($query) {
  $result = sql_select($query);
  return array_map('array_shift', $result);
}

function sql_select_single_cell($query) {
  return array_shift(array_shift(sql_select($query)));
}

?>