/
core_database_API.php
132 lines (117 loc) · 3.62 KB
/
core_database_API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# This program is distributed under the terms and conditions of the GPL
# See the files README and LICENSE for details
# --------------------------------------------------------
# $Revision: 1.11 $
# $Author: vboctor $
# $Date: 2002-06-14 07:50:50 $
#
# $Id: core_database_API.php,v 1.11 2002-06-14 07:50:50 vboctor Exp $
# --------------------------------------------------------
###########################################################################
# Database
###########################################################################
# This in the general interface for all database calls.
# The actual SQL queries are found in the pages.
# Use this as a starting point to port to other databases
# --------------------
# connect to database and select database
function db_connect($p_hostname, $p_username, $p_password, $p_database, $p_port ) {
$t_result = mysql_connect( $p_hostname.':'.$p_port, $p_username, $p_password );
if ( !$t_result ) {
echo 'ERROR: FAILED CONNECTION TO DATABASE: ';
echo db_error();
exit;
}
$t_result = db_select_db( $p_database );
if ( !$t_result ) {
echo 'ERROR: FAILED DATABASE SELECTION: ';
echo db_error();
exit;
}
}
# --------------------
# persistent connect to database and select database
function db_pconnect($p_hostname, $p_username, $p_password, $p_database, $p_port ) {
$t_result = mysql_pconnect( $p_hostname.':'.$p_port, $p_username, $p_password );
if ( !$t_result ) {
echo 'ERROR: FAILED CONNECTION TO DATABASE: ';
echo db_error();
exit;
}
$t_result = db_select_db( $p_database );
if ( !$t_result ) {
echo 'ERROR: FAILED DATABASE SELECTION: ';
echo db_error();
exit;
}
}
# --------------------
# execute query, requires connection to be opened,
function db_query( $p_query ) {
$t_result = mysql_query( $p_query );
if ( !$t_result ) {
echo 'ERROR: FAILED QUERY: '.$p_query.' : ';
echo db_error();
exit;
} else {
return $t_result;
}
}
# --------------------
function db_select_db( $p_db_name ) {
return mysql_select_db( $p_db_name );
}
# --------------------
function db_num_rows( $p_result ) {
return mysql_num_rows( $p_result );
}
# --------------------
function db_fetch_array( $p_result ) {
return mysql_fetch_array( $p_result );
}
# --------------------
function db_result( $p_result, $p_index1=0, $p_index2=0 ) {
if ( $p_result && ( db_num_rows( $p_result ) > 0 ) ) {
return mysql_result( $p_result, $p_index1, $p_index2 );
} else {
return false;
}
}
# --------------------
# return the last inserted id
# For MS SQL use: SELECT @@IDENTITY AS 'id'
function db_insert_id() {
$query = 'SELECT LAST_INSERT_ID()';
$t_result = db_query( $query );
return db_result( $t_result, 0, 0 );
}
# --------------------
function db_error_num() {
return mysql_errno();
}
# --------------------
function db_error_msg() {
return mysql_error();
}
# --------------------
# display both the error num and error msg
function db_error() {
return '<p>'.db_error_num().': '.db_error_msg().'<p>';
}
# --------------------
# close the connection.
# Not really necessary most of the time since a connection is
# automatically closed when a page finishes loading.
function db_close() {
$t_result = mysql_close();
}
# --------------------
if ( OFF == $g_use_persistent_connections ) {
db_connect( $g_hostname, $g_db_username, $g_db_password, $g_database_name, $g_port );
} else {
db_pconnect( $g_hostname, $g_db_username, $g_db_password, $g_database_name, $g_port );
}
?>