Giter Site home page Giter Site logo

michabbb / mysqlihelper Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 43 KB

A simple mysqli helper with lazy-connection, auto-reconnect and named-parameters

License: GNU General Public License v3.0

PHP 100.00%
php mysqli wrapper lazy-connection named-parameters helper

mysqlihelper's Introduction

This is a super simple basic wrapper for mysqli, with some extras:

  • lazy connection
  • auto-reconnect
  • named-parameters

Example usage:

<?php
require 'vendor/autoload.php';

use macropage\MySQLiHelper\MySQLiBase;

$MySQLiHelper = new MySQLiBase(
	[
		'host'    => 'xxxxxxx',
		'port'    => 3306,
		'user'    => 'xxxxxx',
		'pwd'     => 'xxxxxx',
		'db'      => 'xxxxx',
		'charset' => 'utf8',
		'trace'   => true
	]
);
$newQuery = $MySQLiHelper->query('select * from table limit 0,10');
foreach ($newQuery->result as $data) {
	echo $data['table_field']."\n";
}

the defaults are:

  • charset: utf8
  • port: 3306
  • trace: true

The best: this class supports regular AND named parameters - thanks to PEAR:MDB2 where i got the code from :-)

so both works:

$MySQLiHelper->query('select * from table where a=? and b=?',[1,1]);

IS THE SAME LIKE

$MySQLiHelper->query('select * from table where a=:my_placeholder and b=:my_placeholder',['my_placeholder' => 1]);

You can call $MySQLiHelper->connect() manually, but the class uses lazy connection, so as soon as the first query is done, a connection to the mysql server will be established.

In case the connection gets lost or a first connection does not work (whyever), there is a default auto-reconnect of 5 tries with a sleep of 3 seconds. you can change this with:

$MySQLiHelper->setAutoReconnectMaxTry(x)
$MySQLiHelper->setAutoReconnectSleep(x)
$MySQLiHelper->setAutoReconnect(false)
$MySQLiHelper->setLowerTableFields(false)

You need a php developer?
Contact me via

XING
LINKEDIN
TWITTER
G+

....

mysqlihelper's People

Contributors

michabbb avatar

Watchers

 avatar  avatar

mysqlihelper's Issues

Problems with dynamic SQL in PHP8.x

With my select 'SELECT * FROM foo' I get the following error message:

Uncaught ArgumentCountError: mysqli_stmt::bind_result() does not accept unknown named parameters

Fatal error: Uncaught ArgumentCountError: mysqli_stmt::bind_result() does not accept unknown named parameters in /opt/project/vendor/macropage/mysqlihelper/MySQLiBase.php:242
Stack trace:
#0 /opt/project/vendor/macropage/mysqlihelper/MySQLiBase.php(242): mysqli_stmt->bind_result(mant_id: NULL, mant_man_id: NULL, mant_asw_id: NULL, mant_token: NULL, mant_token_type: NULL, mant_db_created: NULL, mant_db_changed: NULL)
#1 /opt/project/db/db.php(229): macropage\MySQLiHelper\MySQLiBase->query('SELECT * FROM l...', Array)

The problem is also described here:
https://stackoverflow.com/questions/66986315/how-to-fix-this-dynamic-sql-query-function-in-php-8

A (first) working solution is:

                $ResponseObj->result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC) ?: null;
                $ResponseObj->numrows = count($ResponseObj->result);

Notice: This block not support lowerTableFields and duplicate counter.

Instead of the current Block at line 225.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.