29 $fullPathToConfigFile = __dir__.
'/dbconfig.php';
30 if (!is_file($fullPathToConfigFile))
32 die(
'The Database configuration file is missing. It has been created during the installation process, but it is not reachable.');
36 require_once ($fullPathToConfigFile);
45 public function connect(): object|bool
48 if (!isset($this->connection))
52 $this->connection = new \mysqli(
53 $this->config[
'server'],
54 $this->config[
'username'],
55 $this->config[
'password'],
56 $this->config[
'dbname'],
61 }
catch (\mysqli_sql_exception $e) {
63 throw new Exception(
'Failed to connect to database: ' . $e->getMessage());
64 }
catch (Exception $e) {
65 throw new Exception(
'Database connection error: ' . $e->getMessage());
75 public function close(): void
77 $this->connection->close();
85 $this->connection->begin_transaction();
90 return $this->connection->multi_query($query);
95 return $this->connection->prepare(
$sql);
98 public function commit(): void
100 $this->connection->commit();
104 return $this->connection->real_escape_string($migrationSql);
116 while ($this->connection->more_results())
118 $this->connection->next_result();
122 $result = $this->connection->rollback();
130 catch (\Exception $e)
144 if (method_exists($this->connection,
'next_result'))
147 return $this->connection->next_result();
152 while ($this->
more_results() && $this->connection->next_result()) {
165 if (method_exists($this->connection,
'more_results'))
168 return $this->connection->more_results() && $this->connection->next_result() && $this->connection->store_result();
185 while ($this->connection->more_results() && $this->connection->next_result())
188 $result = $this->connection->store_result();
199 catch (\Exception $e)
201 error_log(
"Error clearing result sets: " . $e->getMessage());
214 public function query($query)
217 if ($this->connection = $this->
connect())
222 $query = str_replace(
"}",
"", $query);
223 $query = str_replace(
"{", $this->config[
'prefix'], $query);
226 return $this->connection->query($query);
230 die (
'Database error: '.mysqli_connect_error().
'('.mysqli_connect_errno().
')');
239 public function select($query)
254 while ($row =
$result->fetch_assoc())
274 public function error()
277 $this->connection = $this->
connect();
279 return $this->connection->error;
290 $this->connection = $this->
connect();
292 return $this->connection->real_escape_string(
$value);
301 public function import($sqlfile,
$lang)
304 if (!isset($sqlfile) || (empty($sqlfile)))
317 $progressFilename =
$filename.
'_filepointer';
323 if(file_exists($errorFilename) )
330 if(file_exists($progressFilename))
332 $filePosition = file_get_contents($progressFilename);
333 fseek($fp, $filePosition);
338 while($line=fgets($fp, 1024000))
340 if(substr($line,0,2)==
'--' OR trim($line)==
'' )
346 if( substr(trim($query),-1)==
';' )
348 if(!$this->
query($query))
350 $error =
'Error performing query \'<strong>' . $query .
'\':
' . @mysqli_error($this);
351 @file_put_contents($errorFilename, $error."\n");
355 @file_put_contents($progressFilename, ftell($fp)); // save the current file position for
366 // $status .= ftell($fp).'/
'.filesize($filename).' '.(round(ftell($fp)/filesize($filename), 2)*100).'%
'."\n";
376 public function deleteDatabase($database): bool
381 // get all tables as array
382 $result = $this->query("SHOW TABLES IN `$database`");
383 // check if result is set
385 if (isset($result) && (!empty($result)))
387 // walk through result array
388 while ($table = mysqli_fetch_array($result))
389 { // store tablename in var
390 $tableName = $table[0];
391 // try to delete table
392 if ($this->query("DROP TABLE `$database`.`$tableName`"))
394 //echo "$tableName was cleared <br>";
403 // something went wrong
417 public function get_tables()
419 // set tableList array
420 $tableList = array();
421 // get tables from database
422 $res = $this->query("SHOW TABLES");
423 // walk through result
424 while($row = mysqli_fetch_array($res))
426 $tableList[] = $row[0];
436 public function truncateTable(string $table): bool
440 $table = '{
'.$table.'}
';
441 if ($this->query("TRUNCATE TABLE $table"))
458 public function dropTables($tables)
461 // check if table was set
462 if (!isset($tables) || (empty($tables)) || (!array($tables)))
466 foreach ($tables as $table)
468 if ($this->query("DROP TABLE `".$table."`") === true)
print $lang['FILEMAN_UPLOAD']
Mysqli database class; returns db connection object.
quote($value)
Quote and escape value for use in a database query *.
next_result()
Move to next result set of a multi query.
query($query)
Execute any sql query.
real_escape_string($migrationSql)
select($query)
Fetch rows from database (SELECT query)
error()
Fetch last error from the database.
connect()
Connect to the database.
more_results()
Checks if there are more query results from a multi query.
This class serves methods to create backup from files.