Toon posts:

[php] DBI

Pagina: 1
Acties:
  • 76 views sinds 30-01-2008

Verwijderd

Topicstarter
code:
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
Dit script draaide altijd prima, tot dat het verhuist werd naar een andere server...... zowel mysql als php. How come?
<?php
// php-dbi.inc
//
// (C) Craig Knudsen, cknudsen@radix.net, http://www.radix.net/~cknudsen/
// License: GNU GPL (see www.gnu.org)
//
// The functions defined in this file are meant to provide a single
// API to the different PHP database APIs.  Unfortunately, this is
// necessary since PHP does not yet have a common db API.
// The value of $GLOBALS["db_type"] should be defined somewhere
// to one of the following:
//  mysql
//  oracle  (This uses the Oracle8 OCI API, so Oracle 8 libs are required)
//  postgresl
//  odbc
// Limitations:
//  This assumes a single connection to a single database
//  for the sake of simplicity.  Do not make a new query until you
//  are completely finished with the previous one.
//  Rather than use the associative arrays returned with
//  xxx_fetch_array(), normal arrays are used with xxx_fetch_row().
//  (Some db APIs don't support xxx_fetch_array().)
//
// History:
//  22-Apr-2000 Ken Harris <kharris@lhinfo.com>
//          PostgreSQL fixes
//  23-Feb-2000 Craig Knudsen <cknudsen@radix.net>
//          Initial release
//  

// Limitations:
// Fetched rows are returned in non-associative arrays.

// Open up a database connection
// Always do a pooled connection if the db supports it
// For ODBC, $host is ignored, $database = DSN
// For Oracle, $database = tnsnames name
function dbi_connect ( $host, $login, $password, $database ) {
  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
    $c = mysql_pconnect ( $host, $login, $password );
    if ( $c ) {
    if ( ! mysql_select_db ( $database ) )
      return false;
    return $c;
    } else {
    return false;
    }
  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
    if ( strlen ( $host ) && strcmp ( $host, "localhost" ) )
    $c = OCIPLogon ( "$login@$host", $password, $database );
    else
    $c = OCIPLogon ( $login, $password, $database );
    $GLOBALS["oracle_connection"] = $c;
    return $c;
  } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
    $c = pg_connect ( "host=$host dbname=$database user=$login" );
    $GLOBALS["postgresql_connection"] = $c;
    if ( ! $c ) {
      die("Error connecting to database");
      exit;
    }
    return $c;
  } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
    $c = odbc_pconnect ( $database, $login, $password );
    $GLOBALS["odbc_connection"] = $c;
    return $c;
  } else {
    dbi_fatal_error ( "dbi_connect(): db_type not defined." );
  }
}

// Close a database connection
// Not necessary for any database that uses pooled connections
// such as MySQL
function dbi_close ( $conn ) {
  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
    return mysql_close ( $conn );
  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
    return OCILogOff ( $conn );
  } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
    return pg_close ( $GLOBALS["postgresql_connection"] );
  } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
    return odbc_close ( $GLOBALS["odbc_connection"] );
  } else {
    dbi_fatal_error ( "dbi_close(): db_type not defined." );
  }
  
}


// Select the database that all queries should use
//function dbi_select_db ( $database ) {
//  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
//    return mysql_select_db ( $database );
//  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
//    // Not supported.  Must sent up a tnsname and user that uses
//    // the correct tablesapce.
//    return true;
//  } else {
//    dbi_fatal_error ( "dbi_select_db(): db_type not defined." );
//  }
//}

// Execute an SQL query
function dbi_query ( $sql ) {
  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
    return mysql_query ( $sql );
  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
    $GLOBALS["oracle_statement"] =
    OCIParse ( $GLOBALS["oracle_connection"], $sql );
    return OCIExecute ( $GLOBALS["oracle_statement"],
    OCI_COMMIT_ON_SUCCESS );
  } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
    $GLOBALS["postgresql_row"] = 0;
    $GLOBALS["postgresql_row"] = 0;
    $res =  pg_exec ( $GLOBALS["postgresql_connection"], $sql );
    if ( ! $res ) {
      die("Error executing SQL");
      exit;
    }
    $GLOBALS["postgresql_numrows"] = pg_numrows ( $res );
    return $res;
  } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
    $GLOBALS["odbc_row"] = 0;
    return odbc_exec ( $GLOBALS["odbc_connection"], $sql );
  } else {
    dbi_fatal_error ( "dbi_query(): db_type not defined." );
  }
}


// Determine the number of rows from a result
//function dbi_num_rows ( $res ) {
//  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
//    return mysql_num_rows ( $res );
//  } else {
//    dbi_fatal_error ( "dbi_num_rows(): db_type not defined." );
//  }
//}

// Retrieve a single row from the database and return it
// as an array.
// Note: we don't use the more useful xxx_fetch_array because not all
// databases support this function.
function dbi_fetch_row ( $res ) {
  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
    return mysql_fetch_array ( $res );
  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
    if ( OCIFetchInto ( $GLOBALS["oracle_statement"], &$row,
    OCI_NUM + OCI_RETURN_NULLS  ) )
    return $row;
    return 0;
  } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
    if ( $GLOBALS["postgresql_numrows"]  > $GLOBALS["postgresql_row"] ) {
      $r =  pg_fetch_array ( $res, $GLOBALS["postgresql_row"]++ );
      if ( ! $r ) {
        die("Unable to fetch row");
        return '';
      }
    }
    else {
      $r = '';
    }
    return $r;
  } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
    $num_fields = odbc_num_fields ( $res );
    if ( ! odbc_fetch_into ( $res, $GLOBALS["odbc_row"]++, &$ret ) )
    return false;
    return $ret;
  } else {
    dbi_fatal_error ( "dbi_fetch_row(): db_type not defined." );
  }
}


// Free a result set.
// This isn't really necessary for PHP4 since this is done automatically,
// but it's a good habit for PHP3.
function dbi_free_result ( $res ) {
  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
    return mysql_free_result ( $res );
  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
    // Not supported.  Ingore.
    if ( $GLOBALS["oracle_statement"] >= 0 ) {
    OCIFreeStatement ( $GLOBALS["oracle_statement"] );
    $GLOBALS["oracle_statement"] = -1;
    }
  } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
    return pg_freeresult ( $res );
  } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
    return odbc_free_result ( $res );
  } else {
    dbi_fatal_error ( "dbi_free_result(): db_type not defined." );
  }
}


// Get the latest db error message.
function dbi_error () {
  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
    $ret = mysql_error ();
  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
    $ret = OCIError ( $GLOBALS["oracle_connection"] );
  } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
    $ret = pg_errormessage ( $GLOBALS["postgresql_connection"] );
  } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
    // no way to get error from ODBC API
    $ret = "Unknown ODBC error";
  } else {
    $ret = "dbi_error(): db_type not defined.";
  }
  if ( strlen ( $ret ) )
    return $ret;
  else
    return "Unknown error";
}


// display an error message and exit
function dbi_fatal_error ( $msg ) {
  die("<H2>Error</H2>$msg");
  exit;
}

?>

  • Glimi
  • Registratie: Augustus 2000
  • Niet online

Glimi

Designer Drugs

(overleden)
Heb je de sticky gelezen?

  • TheDane
  • Registratie: Oktober 2000
  • Laatst online: 21:14

TheDane

1.618

euhh, :+

wat voor foutmelding krijg je |:(

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

zo werken we hier niet
post een relevant stuk met een duidelijke foutmelding
en lap code hier neerplempen is not done

Doet iets met Cloud (MS/IBM)


  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op donderdag 06 juni 2002 21:15 schreef TheDane het volgende:
euhh, :+

wat voor foutmelding krijg je |:(
Ik durf te wedden dat $GLOBALS de bedoelde waarden niet bevat.

Maar mail es met de makers ofzo.

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

nog een extra linkje gratiz: [topic=512413/1/100]

Doet iets met Cloud (MS/IBM)

Pagina: 1

Dit topic is gesloten.