Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MySQL replacement
/*****************************| MYSQL CONNECTION FUNCTION |*****************************/
* Simple MySQL abstraction layer for the MySQLi absctration as a replacement
* @param resource $mysqli The MySQLi connection link for upgrade to MySQLi
* @param string $query The MySQL query for prepaired statement
* @param array $v The parameters to replace ? in $query. First element must be the type
* @param integer $o Option for more debug infos [0]=no infos(default) [1]=adding debug infos
* @return array [for select]=associative array of table result [for everything else]=associative array with affectedRows,info and insertID
function connectDB($mysqli,$query,$v=array(),$o=0) {
if(substr_count($query,"?")!=strlen($v[0]) || strlen($v[0])!=((count($v)-1)>=0 ? (count($v)-1) : 0)) {
return array('info'=>array('error'=>'Placeholders are unequal! placeholders:'.substr_count($query,"?").', replacements:'.strlen($v[0]).', param:'.(count($v)-1).' ('.$v[0].')')); //error handling here...
for($i=1;$i<count($v);$i++) {
$q=preg_replace("/\?/",(substr($v[0],($i-1),1)=="s" ? '"' : '').'tempplaceholder'.$i.(substr($v[0],($i-1),1)=="s" ? '"' : ''),$q,1);
if(strtolower(substr($q,0,6))=="select") {
if($MySqlres) while($qRes=mysql_fetch_array($MySqlres)) {
else {
if($o===1) { //adding debug infos
if(strtolower(substr($query,0,6))=="update" || strtolower(substr($query,0,6))=="delete") { //optimize at update and delete
preg_match_all('/((update|delete) `(.*)` )/i',$query,$tables);
foreach($tables[3] as $t) mysql_query('OPTIMIZE TABLE `'.$t.'`');
return $results;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.