Update dbwrapper.php

Version 1.6 - New fetchAll functions
This commit is contained in:
2020-05-11 17:23:57 +00:00
parent 1bc58622f2
commit e33b7b1ac3

View File

@@ -1,5 +1,5 @@
<?php <?php
/* DBWrapper.php - Version 1.5 /* DBWrapper.php - Version 1.6
This script is a simple wrapper for SQLite3, MySQL and PgSQL, This script is a simple wrapper for SQLite3, MySQL and PgSQL,
for make possible to use different BD systems without changing the functions. for make possible to use different BD systems without changing the functions.
For use, in dbw_connect you have to specify type of database (see below) For use, in dbw_connect you have to specify type of database (see below)
@@ -71,12 +71,12 @@ function dbw_query($conn,$query){
return $ret; return $ret;
} }
/** Fetch array from query */ /** Fetch array from query (Next result) */
function dbw_fetch_array($conn,$result,$typearray = NULL){ function dbw_fetch_array($conn,$result,$typearray = NULL){
if ($result == false || $result == NULL){return false;} if ($result == false || $result == NULL){return false;}
switch ($conn[1]){ switch ($conn[1]){
case "mysqli": case "mysqli":
if ($typearray == NULL || $typearray == "BOTH"){return mysqli_fetch_array($result);} if ($typearray == NULL || $typearray == "BOTH"){return mysqli_fetch_array($result,MYSQLI_BOTH);}
if ($typearray == "ASSOC"){return mysqli_fetch_array($result,MYSQLI_ASSOC);} if ($typearray == "ASSOC"){return mysqli_fetch_array($result,MYSQLI_ASSOC);}
if ($typearray == "NUM"){return mysqli_fetch_array($result,MYSQLI_NUM);} if ($typearray == "NUM"){return mysqli_fetch_array($result,MYSQLI_NUM);}
break; break;
@@ -93,15 +93,41 @@ function dbw_fetch_array($conn,$result,$typearray = NULL){
} }
} }
/** Make query and fetch array */ /** Make query and fetch all in a array */
function dbw_query_fetch_array($conn,$query){ function dbw_fetch_all($conn,$result,$typearray = NULL){
$time = microtime(true);
$ret=null;
switch ($conn[1]){
case "mysqli":
if ($typearray == NULL || $typearray == "BOTH"){$ret = mysqli_fetch_all($result,MYSQLI_BOTH);}
if ($typearray == "ASSOC"){$ret = mysqli_fetch_all($result,MYSQLI_ASSOC);}
if ($typearray == "NUM"){$ret = mysqli_fetch_all($result,MYSQLI_NUM);}
break;
case "sqlite":
$ret = array();
while ($row = $result->fetchArray()) { //There is not fetchAll in sqlite3, implement it
$ret[] = ($row);
}
break;
case "PgSQL":
$ret = pg_fetch_all($result,PGSQL_ASSOC);
break;
}
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
return $ret;
}
/** Make query and fetch array (For one result) */
function dbw_query_fetch_array($conn,$query,$typearray = NULL){
$time = microtime(true); $time = microtime(true);
$ret=null; $ret=null;
switch ($conn[1]){ switch ($conn[1]){
case "mysqli": case "mysqli":
$result = mysqli_query($conn[0],$query); $result = mysqli_query($conn[0],$query);
if (!$result){return false;} if (!$result){return false;}
$ret = mysqli_fetch_array($result); if ($typearray == NULL || $typearray == "BOTH"){$ret = mysqli_fetch_array($result,MYSQLI_BOTH);}
if ($typearray == "ASSOC"){$ret = mysqli_fetch_array($result,MYSQLI_ASSOC);}
if ($typearray == "NUM"){$ret = mysqli_fetch_array($result,MYSQLI_NUM);}
break; break;
case "sqlite": case "sqlite":
$result = $conn[0]->query($query); $result = $conn[0]->query($query);
@@ -111,13 +137,44 @@ function dbw_query_fetch_array($conn,$query){
case "PgSQL": case "PgSQL":
$result = pg_query($query); $result = pg_query($query);
if (!$result){return false;} if (!$result){return false;}
$ret = pg_fetch_array($result); $ret = pg_fetch_array($result,PGSQL_ASSOC);
break; break;
} }
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>"; //echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
return $ret; return $ret;
} }
/** Make query and fetch all in a array */
function dbw_query_fetch_all($conn,$query,$typearray = NULL){
$time = microtime(true);
$ret=null;
switch ($conn[1]){
case "mysqli":
$result = mysqli_query($conn[0],$query);
if (!$result){return false;}
if ($typearray == NULL || $typearray == "BOTH"){$ret = mysqli_fetch_all($result,MYSQLI_BOTH);}
if ($typearray == "ASSOC"){$ret = mysqli_fetch_all($result,MYSQLI_ASSOC);}
if ($typearray == "NUM"){$ret = mysqli_fetch_all($result,MYSQLI_NUM);}
break;
case "sqlite":
$result = $conn[0]->query($query);
if (!$result){return false;}
$ret = array();
while ($row = $result->fetchArray()) { //There is not fetchAll in sqlite3, implement it
$ret[] = ($row);
}
break;
case "PgSQL":
$result = pg_query($query);
if (!$result){return false;}
$ret = pg_fetch_all($result,PGSQL_ASSOC);
break;
}
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
return $ret;
}
/** Goes a query to $row. $row starts in 0 as first row as if not specified */ /** Goes a query to $row. $row starts in 0 as first row as if not specified */
function dbw_query_goto($conn,$result,$row = 0){ function dbw_query_goto($conn,$result,$row = 0){
switch ($conn[1]){ switch ($conn[1]){