database.mysql.inc 1.69 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
7

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

  mysql_pconnect($url["host"], $url["user"], $url["pass"]) or die(mysql_error());
Kjartan's avatar
Kjartan committed
8
  mysql_select_db(substr($url["path"], 1)) or die("unable to select database");
Dries's avatar
 
Dries committed
9
10
11
12

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

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

// private
function _db_query($query, $debug = 0) {
Dries's avatar
   
Dries committed
40
41
42
43
44
  global $queries;

  if (variable_get("dev_query", 0)) {
    $queries[] = $query;
  }
Dries's avatar
 
Dries committed
45
46
47
48
49
50
51

  $result = mysql_query($query);

  if ($debug) {
    print "<p>query: $query<br />error:". mysql_error() ."</p>";
  }

Dries's avatar
   
Dries committed
52
  if (!mysql_errno()) {
Dries's avatar
 
Dries committed
53
    return $result;
Kjartan's avatar
Kjartan committed
54
55
  }
  else {
Dries's avatar
   
Dries committed
56
    trigger_error(mysql_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
Dries's avatar
 
Dries committed
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
  }
}

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();
}

?>