mirror of
https://gitlab.com/CodeSolutionsProject/DBWrapper.git
synced 2026-02-19 19:41:32 +01:00
Emulating num_rows for sqlite3 when is needed (Sqlite3 doesn't have that natively)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
/* DBWrapper.php - Version 1.4.1
|
/* DBWrapper.php - Version 1.4.2
|
||||||
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)
|
||||||
@@ -49,14 +49,20 @@ function dbw_escape_string($conn,$string){
|
|||||||
|
|
||||||
/** Make query */
|
/** Make query */
|
||||||
function dbw_query($conn,$query){
|
function dbw_query($conn,$query){
|
||||||
|
$time = microtime(true);
|
||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_query($conn[0],$query);
|
$ret = mysqli_query($conn[0],$query);
|
||||||
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
return $conn[0]->query($query);
|
$ret = $conn[0]->query($query);
|
||||||
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
return pg_query($query); //Last error (pg_last_error()) not implemented
|
$ret = pg_query($query);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Fetch array from query */
|
/** Fetch array from query */
|
||||||
@@ -83,23 +89,27 @@ function dbw_fetch_array($conn,$result,$typearray = NULL){
|
|||||||
|
|
||||||
/** Make query and fetch array */
|
/** Make query and fetch array */
|
||||||
function dbw_query_fetch_array($conn,$query){
|
function dbw_query_fetch_array($conn,$query){
|
||||||
|
$time = microtime(true);
|
||||||
|
|
||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
$query = mysqli_query($conn[0],$query);
|
$result = mysqli_query($conn[0],$query);
|
||||||
if ($query == false || $query == NULL){return false;}
|
if (!$result){return false;}
|
||||||
return mysqli_fetch_array($query);
|
$ret = mysqli_fetch_array($result);
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$query = $conn[0]->query($query);
|
$result = $conn[0]->query($query);
|
||||||
if ($query == false || $query == NULL){return false;}
|
if (!$result){return false;}
|
||||||
return $query->fetchArray();
|
$ret = $result->fetchArray();
|
||||||
break;
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
$query = pg_query($query);
|
$result = pg_query($query);
|
||||||
if ($query == false || $query == NULL){return false;}
|
if (!$result){return false;}
|
||||||
return pg_fetch_array($query); //Last error (pg_last_error()) not implemented
|
$ret = pg_fetch_array($result); //Last error (pg_last_error()) not implemented
|
||||||
break;
|
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 */
|
||||||
@@ -155,7 +165,14 @@ function dbw_num_rows($conn,$result){
|
|||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_num_rows($result);
|
return mysqli_num_rows($result);
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
return $result->numRows();
|
//Emulating num_rows. Is faster to use a COUNT(*), but for make it work...
|
||||||
|
$count=0;
|
||||||
|
$result->reset(); //Reset pointer
|
||||||
|
while($row = $result->fetchArray(SQLITE3_ASSOC)){
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
$result->reset(); //Reset pointer
|
||||||
|
return $count;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
return pg_num_rows ($result);
|
return pg_num_rows ($result);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user