Mysqli database class; returns db connection object.
More...
Mysqli database class; returns db connection object.
Database class - connect to mysqli and return connection object
This class establish the database connection if none already exists. It serves some handy methods as well, like quote data, get and delete tables, import .sql files, as well as typical select and query methods.
- Author
- Daniel Retzl danie.nosp@m.lret.nosp@m.zl@gm.nosp@m.ail..nosp@m.com
- Copyright
- 2012-2018 Daniel Retzl yawk.io @license https://opensource.org/licenses/MIT
- Version
- 1.0.0
Definition at line 16 of file db.php.
◆ __construct()
db constructor - Include the database config file
Definition at line 26 of file db.php.
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);
References die.
◆ beginTransaction()
YAWK\db::beginTransaction |
( |
| ) |
|
- Exceptions
-
Definition at line 82 of file db.php.
85 $this->connection->begin_transaction();
◆ clearResults()
YAWK\db::clearResults |
( |
| ) |
|
Clear all pending result sets after a multi-query
- Returns
- bool true if successful, false otherwise
Definition at line 179 of file db.php.
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());
References $result.
◆ close()
Definition at line 74 of file db.php.
77 $this->connection->close();
◆ commit()
Definition at line 97 of file db.php.
100 $this->connection->commit();
◆ connect()
Connect to the database.
- Returns
- object|bool false on failure / mysqli MySQLi object instance on success
- Exceptions
-
Definition at line 44 of file db.php.
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());
References YAWK\db\$connection.
Referenced by YAWK\db\error(), YAWK\db\query(), and YAWK\db\quote().
◆ deleteDatabase()
YAWK\db::deleteDatabase |
( |
|
$database | ) |
|
Delete a whole database (including all tables)
- Parameters
-
Definition at line 375 of file db.php.
388 while ($table = mysqli_fetch_array(
$result))
390 $tableName = $table[0];
392 if ($this->
query(
"DROP TABLE `$database`.`$tableName`"))
query($query)
Execute any sql query.
◆ dropTables()
YAWK\db::dropTables |
( |
|
$tables | ) |
|
Drop table from a database.
- Parameters
-
array | $tables | the tables to drop |
- Returns
- bool
Definition at line 457 of file db.php.
462 if (!isset($tables) || (empty($tables)) || (!array($tables)))
466 foreach ($tables as $table)
468 if ($this->
query(
"DROP TABLE `".$table.
"`") ===
true)
◆ error()
Fetch last error from the database.
- Returns
- string database error
Definition at line 273 of file db.php.
277 $this->connection = $this->
connect();
279 return $this->connection->error;
connect()
Connect to the database.
References YAWK\db\connect().
◆ get_tables()
Get all tables from a database and return as array.
- Returns
- array tableList
Definition at line 416 of file db.php.
420 $tableList = array();
424 while($row = mysqli_fetch_array(
$res))
426 $tableList[] = $row[0];
◆ import()
YAWK\db::import |
( |
|
$sqlfile, |
|
|
|
$lang |
|
) |
| |
Import data from .sql file into database.
- Parameters
-
- Returns
- bool
Definition at line 300 of file db.php.
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";
References $error, $filename, die, and YAWK\db\query().
◆ more_results()
YAWK\db::more_results |
( |
| ) |
|
Checks if there are more query results from a multi query.
- Returns
- bool
Definition at line 162 of file db.php.
165 if (method_exists($this->connection,
'more_results'))
168 return $this->connection->more_results() && $this->connection->next_result() && $this->connection->store_result();
Referenced by YAWK\db\next_result().
◆ multi_query()
YAWK\db::multi_query |
( |
|
$query | ) |
|
Definition at line 87 of file db.php.
90 return $this->connection->multi_query($query);
◆ next_result()
Move to next result set of a multi query.
- Returns
- bool
Definition at line 141 of file db.php.
144 if (method_exists($this->connection,
'next_result'))
147 return $this->connection->next_result();
152 while ($this->
more_results() && $this->connection->next_result()) {
more_results()
Checks if there are more query results from a multi query.
References YAWK\db\more_results().
◆ prepare()
Definition at line 92 of file db.php.
95 return $this->connection->prepare(
$sql);
References $sql.
◆ query()
Execute any sql query.
- Parameters
-
string | $query | the sql query string |
- Returns
- mixed The mysqli result
Definition at line 213 of file db.php.
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().
')');
References YAWK\db\connect(), and die.
Referenced by YAWK\db\import(), and YAWK\db\select().
◆ quote()
Quote and escape value for use in a database query *.
- Parameters
-
string | $value | the value to be quoted and escaped |
- Returns
- string the quoted and escaped string
Definition at line 286 of file db.php.
290 $this->connection = $this->
connect();
292 return $this->connection->real_escape_string(
$value);
References $value, and YAWK\db\connect().
◆ real_escape_string()
YAWK\db::real_escape_string |
( |
|
$migrationSql | ) |
|
Definition at line 102 of file db.php.
104 return $this->connection->real_escape_string($migrationSql);
◆ rollback()
Rollback the current transaction
- Returns
- bool true if rollback succeeded, false otherwise
Definition at line 111 of file db.php.
116 while ($this->connection->more_results())
118 $this->connection->next_result();
122 $result = $this->connection->rollback();
130 catch (\Exception $e)
References $result.
◆ select()
YAWK\db::select |
( |
|
$query | ) |
|
Fetch rows from database (SELECT query)
- Parameters
-
string | $query | the sql query string |
- Returns
- array|bool on failure
Definition at line 238 of file db.php.
254 while ($row =
$result->fetch_assoc())
References $result, $rows, and YAWK\db\query().
◆ truncateTable()
YAWK\db::truncateTable |
( |
string |
$table | ) |
|
Truncate a table.
- Parameters
-
$table | string the table to truncate |
- Returns
- bool
Definition at line 435 of file db.php.
440 $table =
'{'.$table.
'}';
441 if ($this->
query(
"TRUNCATE TABLE $table"))
◆ $config
- Parameters
-
array | $config | mysql configuration (host, username, database etc...) |
Definition at line 19 of file db.php.
◆ $connection
- Parameters
-
object | $connection | holds the mysqli database connection |
Definition at line 21 of file db.php.
Referenced by YAWK\db\connect().
The documentation for this class was generated from the following file:
- /var/www/htdocs/yawk.io/system/classes/db.php