diff --git a/phpminiadmin.php b/phpminiadmin.php index 18d84a0..f115bb0 100644 --- a/phpminiadmin.php +++ b/phpminiadmin.php @@ -88,6 +88,19 @@ loadsess(); +$is_pdo=false; +$pdo_err=''; +if (!function_exists('mysqli_connect')){ + if (class_exists('PDO')) { + $is_pdo=true; + define('MYSQLI_STORE_RESULT',0); + define('MYSQLI_USE_RESULT',1); + }else{ + echo "Please install and enable mysqli or PDO extensions"; + exit; + } +}; + if ($_REQUEST['showcfg']){ print_cfg(); exit; @@ -174,8 +187,8 @@ function display_select($sth,$q){ if ($sth===FALSE or $sth===TRUE) return;#check if $sth is not a mysql resource - $reccount=mysqli_num_rows($sth); - $fields_num=mysqli_field_count($dbh); + $reccount=_db_num_rows($sth); + $fields_num=_db_field_count($dbh, $sth); $w=''; if ($is_sm) $w='sm '; @@ -206,7 +219,7 @@ function display_select($sth,$q){ if ($is_sht) $headers.=""; for($i=0;$i<$fields_num;$i++){ if ($is_sht && $i>0) break; - $meta=mysqli_fetch_field($sth); + $meta=_db_fetch_field($sth, $i); $headers.="
".hs($meta->name)."
"; } if ($is_shd) $headers.="show create databaseshow table statusshow triggers"; @@ -215,7 +228,7 @@ function display_select($sth,$q){ $sqldr.=$headers; $swapper=false; $swp=0; - while($row=mysqli_fetch_row($sth)){ + while($row=db_fetch_row($sth)){ $sqldr.=""; $v=$row[0]; if ($is_sht){ @@ -556,7 +569,115 @@ function print_cfg(){ } -//* utilities +# utilities +# _db* for mysqli/PDO support +function _db_connect($host,$user,$pass,$db,$po,$so){ + global $is_pdo,$pdo_err; + if($is_pdo){ + $dsn="mysql:host=$host;dbname=$db;charset=utf8mb4"; + if($po) $dsn.=";port=$po"; + if($so) $dsn.=";unix_socket=$so"; + $options=array(PDO::ATTR_EMULATE_PREPARES=>false,PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC); + try{ + $pdo_err=''; + return new PDO($dsn,$user,$pass,$options); + }catch(PDOException $e){ + $pdo_err=$e->getMessage(); + } + }else{ + return mysqli_connect($host,$user,$pass,$db,$po,$so); + } +} + +function db_ping($dbh){ + global $is_pdo; + if($is_pdo){ + try{ + $dbh->query('SELECT 1'); + }catch(PDOException $e){ + return false; + } + return true; + }else{ + return mysqli_ping($dbh); + } +} + +function _db_query($dbh,$sql,$resmod){ + global $is_pdo; + if($is_pdo){ + return $dbh->query($sql,$resmod); + }else{ + return mysqli_query($dbh,$sql,$resmod); + } +} + +function db_fetch_row($sth){ + global $is_pdo; + if($is_pdo){ + return $sth->fetch(PDO::FETCH_NUM); + }else{ + return mysqli_fetch_row($sth); + } +} + +function db_fetch_assoc($sth){ + global $is_pdo; + if($is_pdo){ + return $sth->fetch(PDO::FETCH_ASSOC); + }else{ + return mysqli_fetch_assoc($sth); + } +} + +function _db_free($sth){ + global $is_pdo; + if($is_pdo){ + $sth->closeCursor(); + }else{ + mysqli_free_result($sth); + } +} + +function _db_num_rows($sth){ + global $is_pdo; + if($is_pdo){ + return $sth->rowCount(); + }else{ + return mysqli_num_rows($sth); + } +} + +function _db_field_count($dbh,$sth){ + global $is_pdo; + if($is_pdo){ + return $sth->columnCount(); + }else{ + return mysqli_num_fields($sth); + } +} + +function _db_fetch_field($sth,$col){ + global $is_pdo; + if($is_pdo){ + $metadata=$sth->getColumnMeta($col); + $ret=new stdClass(); + $ret->name=$metadata["name"]; + return $ret; + }else{ + return mysqli_fetch_field($sth); + } +} + +function _db_connect_error(){ + global $is_pdo,$pdo_err; + if($is_pdo){ + return $pdo_err; + }else{ + return mysqli_connect_error(); + } +} + function db_connect($nodie=0){ global $dbh,$DB,$err_msg; @@ -568,10 +689,10 @@ function db_connect($nodie=0){ mysqli_ssl_set($dbh,$DB['ssl_key'],$DB['ssl_cert'],$DB['ssl_ca'],NULL,NULL); if (!mysqli_real_connect($dbh,$DB['host'],$DB['user'],$DB['pwd'],$DB['db'],$po,$so,MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT)) $dbh=null; }else{#non-ssl - $dbh=mysqli_connect($DB['host'],$DB['user'],$DB['pwd'],$DB['db'],$po,$so); + $dbh=_db_connect($DB['host'],$DB['user'],$DB['pwd'],$DB['db'],$po,$so); } if (!$dbh) { - $err_msg='Cannot connect to the database because: '.mysqli_connect_error(); + $err_msg='Cannot connect to the database because: '._db_connect_error(); if (!$nodie) die($err_msg); }else{ if ($DB['chset']) db_query("SET NAMES ".$DB['chset']); @@ -583,7 +704,7 @@ function db_connect($nodie=0){ function db_checkconnect($dbh1=NULL, $skiperr=0){ global $dbh; if (!$dbh1) $dbh1=&$dbh; - if (!$dbh1 or !mysqli_ping($dbh1)) { + if (!$dbh1 or !db_ping($dbh1)) { db_connect($skiperr); $dbh1=&$dbh; } @@ -603,34 +724,36 @@ function dbq($s){ function db_query($sql, $dbh1=NULL, $skiperr=0, $resmod=MYSQLI_STORE_RESULT){ $dbh1=db_checkconnect($dbh1, $skiperr); - if($dbh1) $sth=mysqli_query($dbh1, $sql, $resmod); - if(!$sth && $skiperr) return; - if(!$sth) die("Error in DB operation:
\n".mysqli_error($dbh1)."
\n$sql"); + if($dbh1) $sth=_db_query($dbh1, $sql, $resmod); + if (!$sth && $skiperr) return; + if (!$sth) die("Error in DB operation:
\n".mysqli_error($dbh1)."
\n$sql"); return $sth; } function db_array($sql, $dbh1=NULL, $skiperr=0, $isnum=0){#array of rows + global $is_pdo; + if ($is_pdo != 1 && is_null($dbh1)) return null; $sth=db_query($sql, $dbh1, $skiperr, MYSQLI_USE_RESULT); if (!$sth) return; $res=array(); if ($isnum){ - while($row=mysqli_fetch_row($sth)) $res[]=$row; + while($row=db_fetch_row($sth)) $res[]=$row; }else{ - while($row=mysqli_fetch_assoc($sth)) $res[]=$row; + while($row=db_fetch_assoc($sth)) $res[]=$row; } - mysqli_free_result($sth); + _db_free($sth); return $res; } function db_row($sql){ $sth=db_query($sql); - return mysqli_fetch_assoc($sth); + return db_fetch_assoc($sth); } function db_value($sql,$dbh1=NULL,$skiperr=0){ $sth=db_query($sql,$dbh1,$skiperr); if (!$sth) return; - $row=mysqli_fetch_row($sth); + $row=db_fetch_row($sth); return $row[0]; } @@ -658,7 +781,7 @@ function chset_select($sel=''){ $result=''; if ($_SESSION['sql_chset']){ $arr=$_SESSION['sql_chset']; - }else{ + }else { $arr=db_array("show character set",NULL,1); if (!is_array($arr)) $arr=array(array('Charset'=>$DBDEF['chset'])); $_SESSION['sql_chset']=$arr; @@ -873,14 +996,14 @@ function do_export(){ if ($DB['chset']=='utf8mb4') ex_w($BOM); $sth=db_query("select * from `$t[0]`",NULL,0,MYSQLI_USE_RESULT); - $fn=mysqli_field_count($dbh); + $fn=_db_field_count($dbh, $sth); for($i=0;$i<$fn;$i++){ - $m=mysqli_fetch_field($sth); + $m=_db_fetch_field($sth, $i); ex_w(qstr($m->name).(($i<$fn-1)?",":"")); } ex_w($D); - while($row=mysqli_fetch_row($sth)) ex_w(to_csv_row($row)); - mysqli_free_result($sth); + while($row=db_fetch_row($sth)) ex_w(to_csv_row($row)); + _db_free($sth); }else{ ex_start('.sql'); ex_hdr($ctp?$ctp:'text/plain',"$DB[db]".(($ct==1&&$t[0])?".$t[0]":(($ct>1)?'.'.$ct.'tables':'')).".sql$aext"); @@ -890,7 +1013,7 @@ function do_export(){ ex_w("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;$D$D"); $sth=db_query("show full tables from `$DB[db]`"); - while($row=mysqli_fetch_row($sth)){ + while($row=db_fetch_row($sth)){ if (!$rt||array_key_exists($row[0],$th)) do_export_table($row[0],$row[1],$MAXI); } @@ -908,7 +1031,7 @@ function do_export_table($t='',$tt='',$MAXI=838860){ if($_REQUEST['s']){ $sth=db_query("show create table `$t`"); - $row=mysqli_fetch_row($sth); + $row=db_fetch_row($sth); $ct=preg_replace("/\n\r|\r\n|\n|\r/",$D,$row[1]); ex_w("DROP TABLE IF EXISTS `$t`;$D$ct;$D$D"); } @@ -917,7 +1040,7 @@ function do_export_table($t='',$tt='',$MAXI=838860){ $exsql=''; ex_w("/*!40000 ALTER TABLE `$t` DISABLE KEYS */;$D"); $sth=db_query("select * from `$t`",NULL,0,MYSQLI_USE_RESULT); - while($row=mysqli_fetch_row($sth)){ + while($row=db_fetch_row($sth)){ $values=''; foreach($row as $v) $values.=(($values)?',':'').dbq($v); $exsql.=(($exsql)?',':'')."(".$values.")"; @@ -925,7 +1048,7 @@ function do_export_table($t='',$tt='',$MAXI=838860){ ex_w("INSERT INTO `$t` VALUES $exsql;$D");$exsql=''; } } - mysqli_free_result($sth); + _db_free($sth); if ($exsql) ex_w("INSERT INTO `$t` VALUES $exsql;$D"); ex_w("/*!40000 ALTER TABLE `$t` ENABLE KEYS */;$D$D"); }