database.mysql.inc 3.61 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
26
27
28
29
function db_query($query) {
  $args = func_get_args();
  if (count($args) > 1) {
    $args = array_map("check_query", $args);
    $args[0] = $query;
    return _db_query(call_user_func_array("sprintf", $args));
Kjartan's avatar
Kjartan committed
30
31
  }
  else {
Dries's avatar
   
Dries committed
32
33
34
35
36
37
38
39
40
41
42
    return _db_query($query);
  }
}

// debug version
function db_queryd($query) {
  $args = func_get_args();
  if (count($args) > 1) {
    $args = array_map("check_query", $args);
    $args[0] = $query;
    return _db_query(call_user_func_array("sprintf", $args), 1);
Kjartan's avatar
Kjartan committed
43
44
  }
  else {
Dries's avatar
   
Dries committed
45
46
47
48
49
50
    return _db_query($query, 1);
  }
}

// private
function _db_query($query, $debug = 0) {
Dries's avatar
   
Dries committed
51
52
53
  global $queries;

  if (variable_get("dev_query", 0)) {
Dries's avatar
   
Dries committed
54
55
    list($usec, $sec) = explode(" ", microtime());
    $timer = (float)$usec + (float)$sec;
Dries's avatar
   
Dries committed
56
  }
Dries's avatar
 
Dries committed
57
58
59

  $result = mysql_query($query);

Dries's avatar
   
Dries committed
60
61
62
63
64
65
66
  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
67
68
69
70
  if ($debug) {
    print "<p>query: $query<br />error:". mysql_error() ."</p>";
  }

Dries's avatar
   
Dries committed
71
  if (!mysql_errno()) {
Dries's avatar
 
Dries committed
72
    return $result;
Kjartan's avatar
Kjartan committed
73
74
  }
  else {
Dries's avatar
   
Dries committed
75
    trigger_error(mysql_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
Dries's avatar
 
Dries committed
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
  }
}

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
107
108
109
110
111
112
113
114
115
function db_next_id($name) {

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

  db_query("LOCK TABLES sequences WRITE");
  $id = db_result(db_query("SELECT id FROM sequences WHERE name = '%s'", $name)) + 1;
Dries's avatar
   
Dries committed
116
  db_query("REPLACE INTO sequences VALUES ('%s', %d)", $name, $id);
Dries's avatar
   
Dries committed
117
118
119
120
121
  db_query("UNLOCK TABLES");

  return $id;
}

Dries's avatar
   
Dries committed
122
123
124
125
function db_affected_rows() {
  return mysql_affected_rows();
}

Dries's avatar
   
Dries committed
126
/**
Dries's avatar
Dries committed
127
 * Runs a LIMIT query in the database.
Dries's avatar
   
Dries committed
128
 *
Dries's avatar
Dries committed
129
130
131
 * @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
132
 */
Dries's avatar
Dries committed
133
134
135
136
137
138
139
140
141
142
143
144
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);
    $args[0] = $query;
    $query = call_user_func_array("sprintf", $args);
  }
  else {
    $query = func_get_arg(0);
  }
Dries's avatar
   
Dries committed
145
  $query .= " LIMIT $from, $count";
Dries's avatar
Dries committed
146
  return _db_query($query);
Dries's avatar
   
Dries committed
147
148
}

Dries's avatar
Dries committed
149
?>