Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezmysqldb.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezmysqldb.php 9473 2002-04-25 09:11:58Z bf $
5 //
6 // Definition of eZMySQLDB class
7 //
8 // Created on: <19-Jun-2001 16:09:31 bf>
9 //
10 // This source file is part of Exponential Basic, publishing software.
11 //
12 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
27 //
28
31
36 class eZMySQLDB
37 {
38 function __construct( $server, $db, $user, $password )
39 {
40 $this->DB = $db;
41 $this->Server = $server;
42 $this->User = $user;
43 $this->Password = $password;
44
45 $ini =& eZINI::instance( 'site.ini' );
46
47 $socketPath =& $ini->variable( "site", "MySQLSocket" );
48
49 if ( trim( $socketPath != "" ) && $socketPath != "disabled" )
50 {
51 ini_set( "mysql.default_socket", $socketPath );
52 }
53
54 $this->Database = mysqli_connect( $server, $user, $password );
55 $numAttempts = 1;
56 while ( $this->Database == false && $numAttempts < 5 )
57 {
58 sleep(5);
59 $this->Database = mysqli_connect( $server, $user, $password );
60 $numAttempts++;
61 }
62
63 if ( $this->Database == false )
64 {
65 // No reason to continue as nothing will work.
66 print( "<H1>Couldn't connect to database</H1>Please try again later or inform the system administrator." );
67 exit;
68 }
69
70 $ret = mysqli_select_db( $this->Database, $db );
71
72 if ( !$ret )
73 {
74 // No reason to continue as nothing will work.
75 print( "<H1>MySQL Error</H1><br />" . mysqli_errno( $this->Database ) . ": " . mysqli_error( $this->Database )."<br /><hr />Please inform the system administrator." );
76 exit;
77 }
78 }
79
83 function isA()
84 {
85 return "mysql";
86 }
87
92 function &query( $sql, $print=false, $debug=false )
93 {
94 if ( $debug == true )
95 {
96 echo "Executing SQL: $sql<hr>";
97
98 // include_once( "kernel/classes/ezbenchmark.php" );
99
100 $bench = new eZBenchmark();
101 $bench->start();
102
103 $result =& mysqli_query( $this->Database, $sql );
104
105 $bench->stop();
106 if ( $bench->elapsed() > 0.01 )
107 {
108 $GLOBALS["DDD"] .= $sql . "<br>";
109 $GLOBALS["DDD"] .= $bench->printResults( true ) . "<br>";
110 }
111
112 }
113 else
114 {
115 $result =& mysqli_query( $this->Database, $sql );
116 }
117
118 // eZPBLog::writeNotice( $sql );
119
120 $errorMsg = mysqli_error( $this->Database );
121 $errorNum = mysqli_errno( $this->Database );
122
123 if ( $print )
124 {
125 if ( $debug )
126 {
127 print( $sql . "<br>");
128 }
129 }
130
131 if ( $result )
132 {
133 return $result;
134 }
135 else
136 {
137 $this->unlock();
138 $this->Error = "<code>" . htmlentities( $sql ) . "</code><br>\n<b>" . htmlentities(mysqli_error( $this->Database)) . "</b>\n" ;
139 if ( $debug )
140 {
141 print( "<b>MySQL Query Error</b>: " . htmlentities( $sql ) . "<br><b> Error number:</b>" . $errorNum . "<br><b> Error message:</b> ". $errorMsg ."<br>" );
142 }
143 return false;
144 }
145 mysqli_free_result( $result );
146 }
147
158 function array_query( &$array, $sql, $min = 0, $max = -1, $column = false )
159 {
160 $array = array();
161 return $this->array_query_append( $array, $sql, $min, $max, $column );
162 }
163
168 function query_single( &$row, $sql, $column = false )
169 {
170 $array = array();
171 $ret = $this->array_query_append( $array, $sql, 1, 1, $column );
172 if ( isset( $array[0] ) )
173 $row = $array[0];
174 else
175 $row = "";
176 return $ret;
177 }
178
183 function array_query_append( &$array, $sql, $min = 0, $max = -1, $column = false )
184 {
185 $limit = -1;
186 $offset = 0;
187 // check for array parameters
188 if ( is_array( $min ) )
189 {
190 $params = $min;
191
192 if ( isset( $params["Limit"] ) and is_numeric( $params["Limit"] ) )
193 {
194 $limit = $params["Limit"];
195 }
196
197 if ( isset( $params["Offset"] ) and is_numeric( $params["Offset"] ) )
198 {
199 $offset = $params["Offset"];
200 }
201 }
202
203 if ( $limit != -1 )
204 {
205 $sql .= " LIMIT $offset, $limit ";
206 }
207 $result =& $this->query( $sql );
208
209 if ( $result == false )
210 {
211 // print( $this->Error );
212 eZPBLog::writeWarning( $this->Error );
213 return false;
214 }
215
216 $offset = count( $array );
217 // if ( count( $result ) > 0 )
218
219 if ( mysqli_num_rows( $result ) > 0 )
220 {
221 if ( !is_string( $column ) )
222 {
223 for($i = 0; $i < mysqli_num_rows($result); $i++)
224 {
225 $array[$i + $offset] =& mysqli_fetch_array($result);
226 }
227 }
228 else
229 {
230 for($i = 0; $i < mysqli_num_rows($result); $i++)
231 {
232 $tmp_row =& mysqli_fetch_array($result);
233 $array[$i + $offset] =& $tmp_row[$column];
234 }
235 }
236 }
237
238 if ( count( $array ) < $min )
239 {
240 $this->Error = "<code>" . htmlentities( $sql ) . "</code><br>\n<b>" .
241 htmlentities( "Received " . count( $array ) . " rows, minimum is " . (int) $min ) . "</b>\n" ;
242 }
243 if ( $max >= 0 )
244 {
245 if ( count( $array ) > $max )
246 {
247 $this->Error = "<code>" . htmlentities( $sql ) . "</code><br>\n<b>" .
248 htmlentities( "Received " . count( $array ) . " rows, maximum is $max" ) . "</b>\n" ;
249 }
250 }
251 }
252
253
254 function dateToNative( &$date )
255 {
256 $ret = false;
257 if ( is_a( $date, "eZDate" ) )
258 {
259 $ret = $date->year() . "-" . eZDate::addZero( $date->month() ) . "-" . eZDate::addZero( $date->day() );
260 }
261 else
262 print( "Wrong date type, must be an eZDate object." );
263
264 return $ret;
265 }
266
270 function lock( $table )
271 {
272 $this->query( "LOCK TABLES $table WRITE" );
273 }
274
278 function unlock()
279 {
280 $this->query( "UNLOCK TABLES" );
281 }
282
286 function begin()
287 {
288 $this->query( "BEGIN WORK" );
289 }
290
294 function commit()
295 {
296 $this->query( "COMMIT" );
297 }
298
302 function rollback()
303 {
304 $this->query( "ROLLBACK" );
305 }
306
312 function nextID( $table, $field="ID" )
313 {
314 $result = mysqli_query( $this->Database, "SELECT $field FROM $table Order BY $field DESC LIMIT 1" );
315
316 $id = 1;
317 if ( $result )
318 {
319 if ( !mysqli_num_rows( $result ) == 0 )
320 {
321 $array = mysqli_fetch_row( $result );
322 $id = $array[0];
323 $id++;
324 }
325 else
326 $id = 1;
327 }
328 return $id;
329 }
330
334 function escapeString( $str )
335 {
336 if( is_null( $str ) )
337 return false;
338 return mysqli_real_escape_string( $this->Database, $str );
339 }
340
345 function fieldName( $str )
346 {
347 return $str;
348 }
349
353 function close()
354 {
355 mysqli_close( $this->Database );
356 }
357
358 /*
359 will be removed
360 */
361 function insertID()
362 {
363 // print( "insertid is obsolete" );
364 return mysqli_insert_id( $this->Database );
365 }
366
367 function printConnection()
368 {
369 print "Server: " . $this->Server . "<br>\n";
370 print "Database: " . $this->DB . "<br>\n";
371 print "Username: " . $this->User . "<br>\n";
372 print "Password: " . $this->Password . "<br>\n";
373 }
374
375 function counter()
376 {
377 return $this->Counter;
378 }
379
380
381 var $Server;
382 var $DB;
383 var $User;
384 var $Password;
385 var $Database;
386 var $Error;
387 var $Counter;
388
389 }
390
391 ?>
$i
$i
Definition: admin/adlist.php:91
$date
$date
Definition: bugpreview.php:66
eZBenchmark
Definition: ezbenchmark.php:47
eZDate\addZero
addZero( $value)
Definition: ezdate.php:227
eZMySQLDB
Definition: ezmysqldb.php:37
eZMySQLDB\array_query
array_query(&$array, $sql, $min=0, $max=-1, $column=false)
Definition: ezmysqldb.php:158
eZMySQLDB\begin
begin()
Definition: ezmysqldb.php:286
eZMySQLDB\$Password
$Password
Definition: ezmysqldb.php:384
eZMySQLDB\__construct
__construct( $server, $db, $user, $password)
Definition: ezmysqldb.php:38
eZMySQLDB\nextID
nextID( $table, $field="ID")
Definition: ezmysqldb.php:312
eZMySQLDB\array_query_append
array_query_append(&$array, $sql, $min=0, $max=-1, $column=false)
Definition: ezmysqldb.php:183
eZMySQLDB\insertID
insertID()
Definition: ezmysqldb.php:361
eZMySQLDB\counter
counter()
Definition: ezmysqldb.php:375
eZMySQLDB\isA
isA()
Definition: ezmysqldb.php:83
eZMySQLDB\$Database
$Database
Definition: ezmysqldb.php:385
eZMySQLDB\printConnection
printConnection()
Definition: ezmysqldb.php:367
eZMySQLDB\$Server
$Server
Definition: ezmysqldb.php:381
eZMySQLDB\query
& query( $sql, $print=false, $debug=false)
Definition: ezmysqldb.php:92
eZMySQLDB\lock
lock( $table)
Definition: ezmysqldb.php:270
eZMySQLDB\unlock
unlock()
Definition: ezmysqldb.php:278
eZMySQLDB\$Error
$Error
Definition: ezmysqldb.php:386
eZMySQLDB\commit
commit()
Definition: ezmysqldb.php:294
eZMySQLDB\rollback
rollback()
Definition: ezmysqldb.php:302
eZMySQLDB\escapeString
escapeString( $str)
Definition: ezmysqldb.php:334
eZMySQLDB\dateToNative
dateToNative(&$date)
Definition: ezmysqldb.php:254
eZMySQLDB\$Counter
$Counter
Definition: ezmysqldb.php:387
eZMySQLDB\fieldName
fieldName( $str)
Definition: ezmysqldb.php:345
eZMySQLDB\$User
$User
Definition: ezmysqldb.php:383
eZMySQLDB\query_single
query_single(&$row, $sql, $column=false)
Definition: ezmysqldb.php:168
eZMySQLDB\$DB
$DB
Definition: ezmysqldb.php:382
eZMySQLDB\close
close()
Definition: ezmysqldb.php:353
eZPBLog\writeWarning
static writeWarning( $warning)
Definition: ezpblog.php:177
$ini
$ini
Definition: classes/admin/linklist.php:64
$result
$result
Definition: companysearch.php:94
$id
$id
Definition: companytypelist.php:193
$str
$str
Definition: ezarticle/modules/content.php:30
$db
$db
Definition: ezarticle/user/authorlist.php:53
$server
$server
Definition: ezarticle/xmlrpc/xmlrpcserver.php:63
$row
$row
Definition: ezcalendar/user/dayview.php:403
$limit
$limit
Definition: ezimagecatalogue/admin/imagelist.php:312
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
$debug
$debug
Definition: ezstats/admin/cron.php:36
$offset
$offset
Definition: ezstats/admin/cron.php:51
exit
exit
Definition: folder.php:81
$field
$field
Definition: listtable.php:41
Password
</td ></tr >< tr >< td style="color:<?= isset( $error['password1'] ) ?> Password
Definition: step1.php:146
$user
$user
Definition: user/adlist.php:39
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.