1 |
dpavlin |
1.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 |
ravilov |
1.6 |
#echo "<B>[SQL]</B> $query<BR>\n"; |
32 |
ravilov |
1.7 |
# KLUDGY!!! |
33 |
|
|
$query = strtr($query, "ÐÈÆðèæ", "©ÐÈÆ®¹ðèæ¾"); |
34 |
dpavlin |
1.1 |
if( $DB == "pgsql" ) { |
35 |
|
|
$result = pg_exec( $conn, $query ); |
36 |
|
|
} elseif( $DB == "mysql" ) { |
37 |
|
|
$result = mysql_query( $query, $conn ); |
38 |
|
|
} else { |
39 |
|
|
make_error( "DB type is not valid." ); |
40 |
|
|
} |
41 |
|
|
if( ! $result ) { |
42 |
|
|
return 0; |
43 |
|
|
} else { |
44 |
|
|
$array["result"] = $result; |
45 |
|
|
$array["row"] = 0; |
46 |
|
|
return $array; |
47 |
|
|
} |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
function dbi_fetch_array( $result, $row = "" ) |
51 |
|
|
{ |
52 |
|
|
global $DB; |
53 |
|
|
$myresult = $result["result"]; |
54 |
|
|
if( $row == "" ) { |
55 |
|
|
// if no row was specified, fetch the next row in the result set |
56 |
|
|
if( $DB == "pgsql" ) { |
57 |
|
|
$array = pg_fetch_array( $myresult, $result["row"] ); |
58 |
|
|
$result["row"] += 1; |
59 |
|
|
} elseif( $DB == "mysql" ) { |
60 |
|
|
$array = mysql_fetch_array( $myresult ); |
61 |
|
|
} else { |
62 |
|
|
make_error( "DB type is not valid." ); |
63 |
|
|
} |
64 |
|
|
return $array; |
65 |
|
|
} else { |
66 |
|
|
// return the specified row |
67 |
|
|
if( $DB == "pgsql" ) { |
68 |
|
|
$array = pg_fetch_array( $myresult, $row ); |
69 |
|
|
$result["row"] = $row + 1; |
70 |
|
|
} elseif( $DB == "mysql" ) { |
71 |
|
|
$rc = mysql_data_seek( $myresult, $row ); |
72 |
|
|
if( ! $rc ) { |
73 |
|
|
return false; |
74 |
|
|
} |
75 |
|
|
$array = mysql_fetch_array( $myresult ); |
76 |
|
|
$result["row"] = $row + 1; |
77 |
|
|
} else { |
78 |
|
|
make_error( "DB type is not valid." ); |
79 |
|
|
} |
80 |
|
|
return $array; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
function dbi_numrows( $result ) |
85 |
|
|
{ |
86 |
|
|
global $DB; |
87 |
|
|
$myresult = $result["result"]; |
88 |
|
|
|
89 |
|
|
if( $DB == "pgsql" ) { |
90 |
|
|
$n = pg_numrows( $myresult ); |
91 |
|
|
} elseif( $DB == "mysql" ) { |
92 |
|
|
$n = mysql_num_rows( $myresult ); |
93 |
|
|
} else { |
94 |
|
|
make_error( "DB type is not valid." ); |
95 |
|
|
} |
96 |
|
|
return $n; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
function dbi_getlastid( $conn, $result, $tablename, $tablekey ) |
100 |
|
|
{ |
101 |
|
|
global $DB; |
102 |
|
|
if( $DB == "pgsql" ) { |
103 |
|
|
$oid = pg_getlastoid( $result["result"] ); |
104 |
|
|
$result = pg_exec( $conn, "select $tablekey from $tablename where oid = $oid" ); |
105 |
|
|
$array = pg_fetch_row( $result, 0 ); |
106 |
|
|
return( $array[0] ); |
107 |
|
|
} elseif( $DB == "mysql" ) { |
108 |
|
|
return( mysql_insert_id( $conn ) ); |
109 |
|
|
} else { |
110 |
|
|
make_error( "DB type is not valid." ); |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
function dbi_error( $conn ) |
115 |
|
|
{ |
116 |
|
|
global $DB; |
117 |
|
|
if( $DB == "pgsql" ) { |
118 |
|
|
echo "PostgreSQL Error: " . pg_errormessage( $conn ) . "<BR>"; |
119 |
|
|
} elseif( $DB == "mysql" ) { |
120 |
|
|
echo "MySQL Error: " . mysql_errno( $conn ) . " : " . mysql_error( $conn ) . "<BR>"; |
121 |
|
|
} else { |
122 |
|
|
make_error( "DB type is not valid." ); |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
?> |