1 |
<? |
2 |
/* |
3 |
* Phormation |
4 |
* - A library of PHP code to make development of database-driven |
5 |
* html forms easy and quick |
6 |
* |
7 |
* Copyright (C) 2000 Jason D. Hildebrand |
8 |
* PeaceWorks Computer Consulting |
9 |
* |
10 |
* jason@peaceworks.ca |
11 |
* |
12 |
* This program is free software; you can redistribute it and/or modify |
13 |
* it under the terms of the GNU General Public License as published by |
14 |
* the Free Software Foundation; either version 2 of the License, or |
15 |
* (at your option) any later version. |
16 |
* |
17 |
* This program is distributed in the hope that it will be useful, |
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
* GNU General Public License for more details. |
21 |
* |
22 |
* You should have received a copy of the GNU General Public License |
23 |
* along with this program; if not, write to the Free Software |
24 |
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. |
25 |
*/ |
26 |
|
27 |
|
28 |
function dbi_exec( $conn, $query ) |
29 |
{ |
30 |
global $DB; |
31 |
if( $DB == "pgsql" ) { |
32 |
$result = pg_exec( $conn, $query ); |
33 |
} elseif( $DB == "mysql" ) { |
34 |
$result = mysql_query( $query, $conn ); |
35 |
} else { |
36 |
make_error( "DB type is not valid." ); |
37 |
} |
38 |
if( ! $result ) { |
39 |
return 0; |
40 |
} else { |
41 |
$array["result"] = $result; |
42 |
$array["row"] = 0; |
43 |
return $array; |
44 |
} |
45 |
} |
46 |
|
47 |
function dbi_fetch_array( $result, $row = "" ) |
48 |
{ |
49 |
global $DB; |
50 |
$myresult = $result["result"]; |
51 |
if( $row == "" ) { |
52 |
// if no row was specified, fetch the next row in the result set |
53 |
if( $DB == "pgsql" ) { |
54 |
$array = pg_fetch_array( $myresult, $result["row"] ); |
55 |
$result["row"] += 1; |
56 |
} elseif( $DB == "mysql" ) { |
57 |
$array = mysql_fetch_array( $myresult ); |
58 |
} else { |
59 |
make_error( "DB type is not valid." ); |
60 |
} |
61 |
return $array; |
62 |
} else { |
63 |
// return the specified row |
64 |
if( $DB == "pgsql" ) { |
65 |
$array = pg_fetch_array( $myresult, $row ); |
66 |
$result["row"] = $row + 1; |
67 |
} elseif( $DB == "mysql" ) { |
68 |
$rc = mysql_data_seek( $myresult, $row ); |
69 |
if( ! $rc ) { |
70 |
return false; |
71 |
} |
72 |
$array = mysql_fetch_array( $myresult ); |
73 |
$result["row"] = $row + 1; |
74 |
} else { |
75 |
make_error( "DB type is not valid." ); |
76 |
} |
77 |
return $array; |
78 |
} |
79 |
} |
80 |
|
81 |
function dbi_numrows( $result ) |
82 |
{ |
83 |
global $DB; |
84 |
$myresult = $result["result"]; |
85 |
|
86 |
if( $DB == "pgsql" ) { |
87 |
$n = pg_numrows( $myresult ); |
88 |
} elseif( $DB == "mysql" ) { |
89 |
$n = mysql_num_rows( $myresult ); |
90 |
} else { |
91 |
make_error( "DB type is not valid." ); |
92 |
} |
93 |
return $n; |
94 |
} |
95 |
|
96 |
function dbi_getlastid( $conn, $result, $tablename, $tablekey ) |
97 |
{ |
98 |
global $DB; |
99 |
if( $DB == "pgsql" ) { |
100 |
$oid = pg_getlastoid( $result["result"] ); |
101 |
$result = pg_exec( $conn, "select $tablekey from $tablename where oid = $oid" ); |
102 |
$array = pg_fetch_row( $result, 0 ); |
103 |
return( $array[0] ); |
104 |
} elseif( $DB == "mysql" ) { |
105 |
return( mysql_insert_id( $conn ) ); |
106 |
} else { |
107 |
make_error( "DB type is not valid." ); |
108 |
} |
109 |
} |
110 |
|
111 |
function dbi_error( $conn ) |
112 |
{ |
113 |
global $DB; |
114 |
if( $DB == "pgsql" ) { |
115 |
echo "PostgreSQL Error: " . pg_errormessage( $conn ) . "<BR>"; |
116 |
} elseif( $DB == "mysql" ) { |
117 |
echo "MySQL Error: " . mysql_errno( $conn ) . " : " . mysql_error( $conn ) . "<BR>"; |
118 |
} else { |
119 |
make_error( "DB type is not valid." ); |
120 |
} |
121 |
} |
122 |
?> |