database.mysql.inc 3.77 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
  $query = db_prefix_tables($query);
Dries's avatar
 
Dries committed
27 28 29 30
  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
31 32
  }
  else {
Dries's avatar
 
Dries committed
33 34 35 36 37 38 39
    return _db_query($query);
  }
}

// debug version
function db_queryd($query) {
  $args = func_get_args();
Dries's avatar
 
Dries committed
40
  $query = db_prefix_tables($query);
Dries's avatar
 
Dries committed
41 42 43 44
  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
45 46
  }
  else {
Dries's avatar
 
Dries committed
47 48 49 50 51 52
    return _db_query($query, 1);
  }
}

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

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

  $result = mysql_query($query);

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

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

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

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

Dries's avatar
 
Dries committed
116 117 118
  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
119 120 121 122 123
  db_query("UNLOCK TABLES");

  return $id;
}

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

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

Dries's avatar
Dries committed
153
?>