mirror of
https://gitlab.com/CodeSolutionsProject/DBWrapper.git
synced 2026-02-22 12:53:45 +01:00
Update dbwrapper.php
Version 1.6 - New fetchAll functions
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
/* DBWrapper.php - Version 1.5
|
||||
/* DBWrapper.php - Version 1.6
|
||||
This script is a simple wrapper for SQLite3, MySQL and PgSQL,
|
||||
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)
|
||||
@@ -71,12 +71,12 @@ function dbw_query($conn,$query){
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/** Fetch array from query */
|
||||
/** Fetch array from query (Next result) */
|
||||
function dbw_fetch_array($conn,$result,$typearray = NULL){
|
||||
if ($result == false || $result == NULL){return false;}
|
||||
switch ($conn[1]){
|
||||
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 == "NUM"){return mysqli_fetch_array($result,MYSQLI_NUM);}
|
||||
break;
|
||||
@@ -93,15 +93,41 @@ function dbw_fetch_array($conn,$result,$typearray = NULL){
|
||||
}
|
||||
}
|
||||
|
||||
/** Make query and fetch array */
|
||||
function dbw_query_fetch_array($conn,$query){
|
||||
/** Make query and fetch all in a array */
|
||||
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);
|
||||
$ret=null;
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
$result = mysqli_query($conn[0],$query);
|
||||
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;
|
||||
case "sqlite":
|
||||
$result = $conn[0]->query($query);
|
||||
@@ -111,13 +137,44 @@ function dbw_query_fetch_array($conn,$query){
|
||||
case "PgSQL":
|
||||
$result = pg_query($query);
|
||||
if (!$result){return false;}
|
||||
$ret = pg_fetch_array($result);
|
||||
$ret = pg_fetch_array($result,PGSQL_ASSOC);
|
||||
break;
|
||||
}
|
||||
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
|
||||
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 */
|
||||
function dbw_query_goto($conn,$result,$row = 0){
|
||||
switch ($conn[1]){
|
||||
@@ -227,4 +284,4 @@ function dbw_result_seek($conn,$result){return dbw_query_goto($conn,$result,$row
|
||||
|
||||
function dbw_insert_id($conn){return dbw_last_id($conn);}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user