database.mysql.inc 2.5 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
8
9
10
11
  // Allow for non-standard MySQL port.
  if ($url["port"]) {
     $url["host"] = $url["host"] . ":" . $url["port"];
  }
  
Dries's avatar
 
Dries committed
12
  mysql_pconnect($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
16
17

  // NOTE: we are using a persistent connection!
}

Dries's avatar
   
Dries committed
18
19
20
21
22
23
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
24
25
  }
  else {
Dries's avatar
   
Dries committed
26
27
28
29
30
31
32
33
34
35
36
    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
37
38
  }
  else {
Dries's avatar
   
Dries committed
39
40
41
42
43
44
    return _db_query($query, 1);
  }
}

// private
function _db_query($query, $debug = 0) {
Dries's avatar
   
Dries committed
45
46
47
  global $queries;

  if (variable_get("dev_query", 0)) {
Dries's avatar
   
Dries committed
48
49
    list($usec, $sec) = explode(" ", microtime());
    $timer = (float)$usec + (float)$sec;
Dries's avatar
   
Dries committed
50
  }
Dries's avatar
 
Dries committed
51
52
53

  $result = mysql_query($query);

Dries's avatar
   
Dries committed
54
55
56
57
58
59
60
  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
61
62
63
64
  if ($debug) {
    print "<p>query: $query<br />error:". mysql_error() ."</p>";
  }

Dries's avatar
   
Dries committed
65
  if (!mysql_errno()) {
Dries's avatar
 
Dries committed
66
    return $result;
Kjartan's avatar
Kjartan committed
67
68
  }
  else {
Dries's avatar
   
Dries committed
69
    trigger_error(mysql_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
Dries's avatar
 
Dries committed
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
  }
}

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
101
102
103
104
105
106
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;
  db_query("REPLACE INTO sequences VALUES ('%s', '%d')", $name, $id);
  db_query("UNLOCK TABLES");

  return $id;
}

Dries's avatar
   
Dries committed
116
117
118
119
120
function db_affected_rows() {
  return mysql_affected_rows();
}

?>