database.mysql.inc 4.08 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Kjartan's avatar
Kjartan committed
2
// $Id$
Dries's avatar
 
Dries committed
3
4
5
6

function db_connect($url) {
  $url = parse_url($url);

7
  // Allow for non-standard MySQL port.
Dries's avatar
   
Dries committed
8
  if (isset($url["port"])) {
9
10
     $url["host"] = $url["host"] . ":" . $url["port"];
  }
Dries's avatar
   
Dries committed
11

Dries's avatar
   
Dries committed
12
  mysql_connect($url["host"], $url["user"], $url["pass"]) or die(mysql_error());
Kjartan's avatar
Kjartan committed
13
  mysql_select_db(substr($url["path"], 1)) or die("unable to select database");
Dries's avatar
 
Dries committed
14

15
  /*
Dries's avatar
   
Dries committed
16
  ** Note that you can change the 'mysql_connect' statement to 'mysql_pconnect'
17
  ** if you want to use persistent connections.  This is not recommended on
Dries's avatar
   
Dries committed
18
  ** shared hosts, might require additional database/webserver tuning but
19
  ** increases performance when the overhead to connect to your database is
Dries's avatar
   
Dries committed
20
21
  ** high (eg. your database and webserver live on different machines).
  */
Dries's avatar
 
Dries committed
22
23
}

Dries's avatar
   
Dries committed
24
25
function db_query($query) {
  $args = func_get_args();
Dries's avatar
   
Dries committed
26

Dries's avatar
   
Dries committed
27
  $query = db_prefix_tables($query);
Dries's avatar
   
Dries committed
28
  if (count($args) > 1) {
Dries's avatar
   
Dries committed
29
30
31
32
33
34
35
36
37
    if(is_array($args[1])){
      $args1 = array_map("check_query", $args[1]);
      $nargs = array_merge(array($query), $args1);
    }
    else {
      $nargs = array_map("check_query", $args);
      $nargs[0] = $query;
    }
    return _db_query(call_user_func_array("sprintf", $nargs));
Kjartan's avatar
Kjartan committed
38
39
  }
  else {
Dries's avatar
   
Dries committed
40
41
42
43
44
45
46
    return _db_query($query);
  }
}

// debug version
function db_queryd($query) {
  $args = func_get_args();
Dries's avatar
   
Dries committed
47
  $query = db_prefix_tables($query);
Dries's avatar
   
Dries committed
48
  if (count($args) > 1) {
Dries's avatar
   
Dries committed
49
50
51
52
53
54
55
56
57
    if(is_array($args[1])){
      $args1 = array_map("check_query", $args[1]);
      $nargs = array_merge(array($query), $args1);
    }
    else {
      $nargs = array_map("check_query", $args);
      $nargs[0] = $query;
    }
    return _db_query(call_user_func_array("sprintf", $nargs), 1);
Kjartan's avatar
Kjartan committed
58
59
  }
  else {
Dries's avatar
   
Dries committed
60
61
62
63
64
65
    return _db_query($query, 1);
  }
}

// private
function _db_query($query, $debug = 0) {
Dries's avatar
   
Dries committed
66
67
68
  global $queries;

  if (variable_get("dev_query", 0)) {
Dries's avatar
   
Dries committed
69
70
    list($usec, $sec) = explode(" ", microtime());
    $timer = (float)$usec + (float)$sec;
Dries's avatar
   
Dries committed
71
  }
Dries's avatar
 
Dries committed
72
73
74

  $result = mysql_query($query);

Dries's avatar
   
Dries committed
75
76
77
78
79
80
81
  if (variable_get("dev_query", 0)) {
    list($usec, $sec) = explode(" ", microtime());
    $stop = (float)$usec + (float)$sec;
    $diff = $stop - $timer;
    $queries[] = array($query, $diff);
  }

Dries's avatar
 
Dries committed
82
83
84
85
  if ($debug) {
    print "<p>query: $query<br />error:". mysql_error() ."</p>";
  }

Dries's avatar
   
Dries committed
86
  if (!mysql_errno()) {
Dries's avatar
 
Dries committed
87
    return $result;
Kjartan's avatar
Kjartan committed
88
89
  }
  else {
Dries's avatar
   
Dries committed
90
    trigger_error(mysql_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
Dries's avatar
 
Dries committed
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
  }
}

function db_fetch_object($result) {
  if ($result) {
    return mysql_fetch_object($result);
  }
}

function db_fetch_array($result) {
  if ($result) {
    return mysql_fetch_array($result, MYSQL_ASSOC);
  }
}

function db_num_rows($result) {
  if ($result) {
    return mysql_num_rows($result);
  }
}

function db_result($result, $row = 0) {
  if ($result && mysql_num_rows($result) > $row) {
    return mysql_result($result, $row);
  }
}

function db_error() {
  return mysql_errno();
}

Dries's avatar
   
Dries committed
122
123
124
125
126
127
128
function db_next_id($name) {

  /*
  ** Note that REPLACE query below correctly creates a new sequence
  ** when needed
  */

Dries's avatar
   
Dries committed
129
130
131
  db_query("LOCK TABLES {sequences} WRITE");
  $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
  db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
Dries's avatar
   
Dries committed
132
133
134
135
136
  db_query("UNLOCK TABLES");

  return $id;
}

Dries's avatar
   
Dries committed
137
138
139
140
function db_affected_rows() {
  return mysql_affected_rows();
}

Dries's avatar
   
Dries committed
141
/**
Dries's avatar
Dries committed
142
 * Runs a LIMIT query in the database.
Dries's avatar
   
Dries committed
143
 *
Dries's avatar
Dries committed
144
145
146
 * @param   mixed     $query  SQL query, followed by a variable number of arguments which are substituted into query by sprintf, followed by 'from' and 'count'  parameters.  'from' is the row to start fetching, 'count' the numbers of rows to fetch.
 * @return  resource  a MySQL result or FALSE if the query was not executed correctly.
 * @access  public
Dries's avatar
   
Dries committed
147
 */
Dries's avatar
Dries committed
148
149
150
151
152
153
function db_query_range($query) {
  $args = func_get_args();
  $count = array_pop($args);
  $from = array_pop($args);
  if (count(func_get_args()) > 3) {
    $args = array_map("check_query", $args);
Dries's avatar
   
Dries committed
154
    $query = db_prefix_tables($query);
Dries's avatar
Dries committed
155
156
157
158
159
    $args[0] = $query;
    $query = call_user_func_array("sprintf", $args);
  }
  else {
    $query = func_get_arg(0);
Dries's avatar
   
Dries committed
160
    $query = db_prefix_tables($query);
Dries's avatar
Dries committed
161
  }
Dries's avatar
   
Dries committed
162
  $query .= " LIMIT $from, $count";
Dries's avatar
Dries committed
163
  return _db_query($query);
Dries's avatar
   
Dries committed
164
165
}

Dries's avatar
Dries committed
166
?>