Mainly for development and logging of queries, but now that the class is up and running future releases should be expected to do the heavy lifting of queries and iteration.
Mysqli Abstraction Layer v1.9.0
Mysqli2 is an enhanced wrapper around PHP’s native MySQLi extension that provides simplified prepared statement execution, better error handling, and development/production mode switching. The class extends mysqli, inheriting all native MySQLi methods while adding streamlined functionality.
Key Features
- Ny refaktorert klasse, nye metoder. Se docs/mysqli2_documentation.md
- Breaking file into smaller files, better readability.
- Updated readme.
- Bugfix, error_number has to be int
- buddy() updated, has prepared output aswell. echo $mysqli->buddy('table','insert','prepared');
- parse_col_type, added prepared for type
- Added mode for ->result('assoc') without using second parameter.
- Updated for PHP 8.1
To install the library use composer:
composer require steinhaug/mysqli
// Enable development mode (verbose errors)
Mysqli2::isDev(true);
// Get singleton instance with connection parameters
$mysqli = Mysqli2::getInstance($mysql_host, $mysql_port, $mysql_user, $mysql_password, $mysql_database);
// Set character encoding
$mysqli->set_charset("utf8");
// Check connection
if ($mysqli->connect_errno) {
echo 'Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
exit();
}
Prepared query, quick query returns associated array:
$TestID = 1;
$row = $mysqli->prepared_query1('SELECT * FROM `zzz_testtable` WHERE `TestID`=?', 'i', [$TestID], true);
if($row===null){
throw new Exception('prepared_query1(sql,true) error');
}
Prepared query, results comes in array
$TestID = 5;
$resultset = $mysqli->prepared_query('SELECT * FROM `zzz_testtable` WHERE `TestID`=?', 'i', [$TestID]);
if( !count($resultset) ){
throw new Exception('prepared_query() returned unexpected result');
}
// echo $resultset[0]
Prepared delete:
$TestID = 1;
$UserID = 1;
$affected_rows = $mysqli->prepared_query('DELETE FROM `zzz_testtable` WHERE `TestID`=? AND `user_id`=?', 'ii', [$TestID, $UserID]);
if (!$affected_rows) {
throw new Exception('prepared_query(delete from...) reported 0 deletion');
}
Prepared insert:
$sql = [
'INSERT INTO `table_name` (`col_name`, `col_name_two`, `col_name_three`, `col_name_four`, `col_name_five`) VALUES (?,?,?,?,?)',
'issds',
[$variable, '2020-01-01 00:00:00', 'test/test@test.com', 1.23, '2020-01-01 00:00:00'],
];
$InsertId = $mysqli->prepared_insert($sql);
if (!$InsertId) {
throw new Exception('prepared_insert(insert into) inserted_id error');
}
This project is licensed under the terms of the MIT License. Enjoy!
Kim Steinhaug, steinhaug at gmail dot com.
Sosiale lenker: LinkedIn, SoundCloud, Instagram, Youtube, X, Ko-Fi, Github, Gitlab
Generative AI lenker: Udio, Suno, Huggingface
Resurser og hjelpesider: Linktr.ee/steinhaugai, Linktr.ee/stainhaug, pinterest/steinhaug, pinterest/stainhaug