| 1 |
<?php
|
| 2 |
// $Id: database.mysql.inc,v 1.1.2.3 2009/02/04 01:59:00 daften Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Mantis database interface code for MySQL database servers.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function _bugbits_db_connect($username, $password, $database_name, $host = 'localhost', $port = NULL) {
|
| 10 |
// Check if MySQL support is present in PHP
|
| 11 |
if (!function_exists('mysql_connect')) {
|
| 12 |
watchdog("Bugbits", "Unable to use the MySQL database (for Mantis) because the MySQL extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.", array(), WATCHDOG_ERROR);
|
| 13 |
}
|
| 14 |
|
| 15 |
// Allow for non-standard MySQL port.
|
| 16 |
if (isset($port)) {
|
| 17 |
$host = $host .':'. $port;
|
| 18 |
}
|
| 19 |
|
| 20 |
// - TRUE makes mysql_connect() always open a new link, even if
|
| 21 |
// mysql_connect() was called before with the same parameters.
|
| 22 |
// This is important if you are using two databases on the same
|
| 23 |
// server.
|
| 24 |
// - 2 means CLIENT_FOUND_ROWS: return the number of found
|
| 25 |
// (matched) rows, not the number of affected rows.
|
| 26 |
$connection = @mysql_connect($host, $username, $password, TRUE, 2);
|
| 27 |
if (!$connection || !mysql_select_db($database_name)) {
|
| 28 |
// Show error screen otherwise
|
| 29 |
watchdog("Bugbits", mysql_error(), array(), WATCHDOG_ERROR);
|
| 30 |
}
|
| 31 |
|
| 32 |
// Force UTF-8.
|
| 33 |
mysql_query('SET NAMES "utf8"', $connection);
|
| 34 |
return $connection;
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Runs a basic query in the active database.
|
| 39 |
*
|
| 40 |
* @param $query
|
| 41 |
* A string containing an SQL query.
|
| 42 |
*
|
| 43 |
* @return
|
| 44 |
* A database query result resource, or FALSE if the query was not
|
| 45 |
* executed correctly.
|
| 46 |
*/
|
| 47 |
function _bugbits_db_query($query) {
|
| 48 |
$query = _bugbits_db_prefix_tables($query);
|
| 49 |
return mysql_query($query);
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Fetch one result row from the previous query as an object.
|
| 54 |
*
|
| 55 |
* @param $result
|
| 56 |
* A database query result resource, as returned from _bugbits_db_query().
|
| 57 |
* @return
|
| 58 |
* An object representing the next row of the result, or FALSE. The attributes
|
| 59 |
* of this object are the table fields selected by the query.
|
| 60 |
*/
|
| 61 |
function _bugbits_db_fetch_object($result) {
|
| 62 |
if ($result) {
|
| 63 |
return mysql_fetch_object($result);
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Return an individual result field from the previous query.
|
| 69 |
*
|
| 70 |
* Only use this function if exactly one field is being selected; otherwise,
|
| 71 |
* use db_fetch_object() or db_fetch_array().
|
| 72 |
*
|
| 73 |
* @param $result
|
| 74 |
* A database query result resource, as returned from db_query().
|
| 75 |
* @return
|
| 76 |
* The resulting field or FALSE.
|
| 77 |
*/
|
| 78 |
function _bugbits_db_result($result) {
|
| 79 |
if ($result && mysql_num_rows($result) > 0) {
|
| 80 |
// The mysql_fetch_row function has an optional second parameter $row
|
| 81 |
// but that can't be used for compatibility with Oracle, DB2, etc.
|
| 82 |
$array = mysql_fetch_row($result);
|
| 83 |
return $array[0];
|
| 84 |
}
|
| 85 |
return FALSE;
|
| 86 |
}
|