mirror of
https://gitlab.com/CodeSolutionsProject/DBWrapper.git
synced 2026-02-23 13:23:49 +01:00
Version 1.4.1, TODO functions and wrapper to query_goto
This commit is contained in:
20
README.md
20
README.md
@@ -1,4 +1,4 @@
|
|||||||
# DBWrapper - Version 1.4
|
# DBWrapper - Version 1.4.1
|
||||||
|
|
||||||
This script is a simple wrapper for SQLite3, MySQL and PgSQL, for make possible to use different BD systems without changing the functions.
|
This script is a simple wrapper for SQLite3, MySQL and PgSQL, for make possible to use different BD systems without changing the functions.
|
||||||
|
|
||||||
@@ -21,25 +21,27 @@ This script is a simple wrapper for SQLite3, MySQL and PgSQL, for make possible
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
//Does a query (array, or false if error)
|
//Does a query (array, or false if error)
|
||||||
array dbw_query (resource $conn, string $query);
|
resource dbw_query (resource $conn, string $query);
|
||||||
//This does a (non interactive) multiquery. Its used for load from a file/script for example.
|
//This does a (non interactive) multiquery. Its used for load from a file/script for example.
|
||||||
bool dbw_multi_query(resource $conn, string $multiquery);
|
bool dbw_multi_query(resource $conn, string $multiquery);
|
||||||
//This do a query and fetch array, all in one function($typearray optional, see below)
|
//This do a query and fetch array, all in one function($typearray optional, see below)
|
||||||
array dbw_query_fetch_array (resource $conn,string $query[, string $typearray]);
|
resource dbw_query_fetch_array (resource $conn,string $query[, string $typearray]);
|
||||||
```
|
```
|
||||||
|
|
||||||
* Using result of a query
|
* Using result of a query
|
||||||
|
|
||||||
```php
|
```php
|
||||||
//Fetch a row. ($typearray optional, see below)
|
//Fetch a row. ($typearray optional, see below)
|
||||||
array dbw_fetch_array (resource $conn,array $result[, string $typearray]);
|
array dbw_fetch_array (resource $conn,resource $result[, string $typearray]);
|
||||||
//Wrappers of dbw_fetch_array with row or assoc arguments
|
//Wrappers of dbw_fetch_array with row or assoc arguments
|
||||||
array dbw_fetch_row(resource $conn,array $result);
|
array dbw_fetch_row(resource $conn,resource $result);
|
||||||
array dbw_fetch_assoc(resource $conn,array $result);
|
array dbw_fetch_assoc(resource $conn,resource $result);
|
||||||
//Goto X result of a query. If row is not specified, will be first row, 0
|
//Goto X result of a query (Not retrieve it). If row is not specified, will be first row, 0
|
||||||
bool array dbw_query_goto(resource $conn,array $result[,int $row]) ;
|
bool dbw_query_goto(resource $conn,resource $result[,int $row]);
|
||||||
|
// Wrappers to dbw_query_goto are dbw_data_seek and dbw_result_seek for compatibility
|
||||||
|
|
||||||
//Return number of results of a query
|
//Return number of results of a query
|
||||||
int dbw_num_rows(resource $conn, array $result);
|
int dbw_num_rows(resource $conn, resource $result);
|
||||||
```
|
```
|
||||||
|
|
||||||
* Without a query
|
* Without a query
|
||||||
|
|||||||
@@ -1,43 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
/* DBWrapper.php - Version 1.4
|
/* DBWrapper.php - Version 1.4.1
|
||||||
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)
|
||||||
Avaiable functions:
|
Read README.md for details of every function and use
|
||||||
dbw_connect ($tdb,$server,$database,$user,$password) -> some values are optional,
|
|
||||||
except $tdb and $server. $server is location in SQLite3
|
|
||||||
dbw_close ($conn) -> Closes connection
|
|
||||||
|
|
||||||
dbw_query ($conn,$query) -> Does a query
|
|
||||||
dbw_multi_query($conn,$multiquery) -> This does a multiquery without returning nothing.
|
|
||||||
Its used for load from a file/script
|
|
||||||
dbw_query_fetch_array ($conn,$query[,$typearray]) -> This do a query and fetch array, all in one function
|
|
||||||
($typearray optional, see below)
|
|
||||||
|
|
||||||
--After here, this functions works with result of a query--
|
|
||||||
|
|
||||||
dbw_fetch_array ($conn,$result[,$typearray]) -> Fetch a row. ($typearray optional, see below)
|
|
||||||
dbw_escape_string($conn,$string) -> Escapes conflictive chars for inserting into database
|
|
||||||
|
|
||||||
dbw_fetch_row and dbw_fetch_assoc ($conn,$result) -> Wrappers of dbw_fetch_array with row or assoc arguments
|
|
||||||
dbw_query_goto($conn,$result[,$row]) -> Goto X result of a query. If row is not specified, will be first row, 0
|
|
||||||
dbw_num_rows($conn,$result) -> Return number of results of a query
|
|
||||||
|
|
||||||
--This doesnt need a query--
|
|
||||||
dbw_last_id($conn) -> Returns last insert ID
|
|
||||||
dbw_insert_id($conn) -> Alias of dbw_last_id
|
|
||||||
|
|
||||||
|
|
||||||
$tdb (Type of database) can be:
|
|
||||||
-mysql/mysqli -> MySQL or MariaDB
|
|
||||||
-sqlite/sqlite3 -> Sqlite3
|
|
||||||
-PostgreSQL/PgSQL/pg -> PostgreSQL
|
|
||||||
|
|
||||||
$conn is the connection stablished in dbw_connect (ie. $conn = dbw_connect('sqlite','file.sqlite'))
|
|
||||||
$typearray is the form of array is returned, and not writed is default:
|
|
||||||
-ASSOC -> Associative indexes
|
|
||||||
-NUM -> Numeric indexes
|
|
||||||
-BOTH -> (Default) Both types of indexes
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -46,24 +12,24 @@ function dbw_connect($tdb,$server,$database=NULL,$user = NULL,$password=NULL){
|
|||||||
switch ($tdb){
|
switch ($tdb){
|
||||||
case "mysql":
|
case "mysql":
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
$return[0] = mysqli_connect($server,$user,$password,$database) or die("Error de conexion");
|
$return[0] = mysqli_connect($server,$user,$password,$database) or die("Error de conexion");
|
||||||
$return[1] = "mysqli"; //Return standard mysqli for other funcs.
|
$return[1] = "mysqli"; //Return standard mysqli for other funcs.
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
case "sqlite3":
|
case "sqlite3":
|
||||||
$return[0] = new SQLite3($server);
|
$return[0] = new SQLite3($server);
|
||||||
if (!$return[0]) die ("Error de conexion");
|
if (!$return[0]) die ("Error de conexion");
|
||||||
$return[1] = "sqlite"; //Return standard SQLite3 for other funcs.
|
$return[1] = "sqlite"; //Return standard SQLite3 for other funcs.
|
||||||
break;
|
break;
|
||||||
case "PostgreSQL":
|
case "PostgreSQL":
|
||||||
case "pg":
|
case "pg":
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
$return[0] = pg_connect("host=$server dbname=$database user=$user password=$password") or die ('Error de conexion: ' . pg_last_error());
|
$return[0] = pg_connect("host=$server dbname=$database user=$user password=$password") or die ('Error de conexion: ' . pg_last_error());
|
||||||
$return[1] = "PgSQL"; //Return standard PgSQL for other funcs.
|
$return[1] = "PgSQL"; //Return standard PgSQL for other funcs.
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
@@ -86,16 +52,10 @@ function dbw_query($conn,$query){
|
|||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_query($conn[0],$query);
|
return mysqli_query($conn[0],$query);
|
||||||
break;
|
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
return $conn[0]->query($query);
|
return $conn[0]->query($query);
|
||||||
break;
|
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
return pg_query($query); //Last error (pg_last_error()) not implemented
|
return pg_query($query); //Last error (pg_last_error()) not implemented
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,9 +78,6 @@ function dbw_fetch_array($conn,$result,$typearray = NULL){
|
|||||||
if ($typearray == "ASSOC"){return pg_fetch_array($result,NULL,PGSQL_ASSOC);}
|
if ($typearray == "ASSOC"){return pg_fetch_array($result,NULL,PGSQL_ASSOC);}
|
||||||
if ($typearray == "NUM"){return pg_fetch_array($result,NULL,PGSQL_NUM);}
|
if ($typearray == "NUM"){return pg_fetch_array($result,NULL,PGSQL_NUM);}
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,17 +107,17 @@ function dbw_query_goto($conn,$result,$row = 0){
|
|||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
mysqli_data_seek($result,$row);
|
mysqli_data_seek($result,$row);
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$result->reset();
|
$result->reset();
|
||||||
$count = 0;
|
$count = 0;
|
||||||
while ($count != $row){
|
while ($count != $row){
|
||||||
$result->fetchArray();
|
$result->fetchArray();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
pg_result_seek($result, $row);
|
pg_result_seek($result, $row);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,13 +126,13 @@ function dbw_multi_query($conn,$query){
|
|||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
mysqli_multi_query($conn[0],$query);
|
mysqli_multi_query($conn[0],$query);
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$conn[0]->exec($query);
|
$conn[0]->exec($query);
|
||||||
break;
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
die ("No soportado aun"); // TODO
|
$null = pg_query($query);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,13 +141,10 @@ function dbw_last_id($conn){
|
|||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_insert_id($conn[0]);
|
return mysqli_insert_id($conn[0]);
|
||||||
break;
|
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
return $conn[0]->lastInsertRowID();
|
return $conn[0]->lastInsertRowID();
|
||||||
break;
|
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
return pg_fetch_array(pg_query("SELECT lastval();"))[0];
|
return pg_fetch_array(pg_query("SELECT lastval();"))[0];
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,13 +154,10 @@ function dbw_num_rows($conn,$result){
|
|||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_num_rows($result);
|
return mysqli_num_rows($result);
|
||||||
break;
|
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
die ("No soportado aun"); // TODO
|
return $result->numRows();
|
||||||
break;
|
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
die ("No soportado aun"); // TODO
|
return pg_num_rows ($result);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,13 +167,13 @@ function dbw_close($conn){
|
|||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
mysqli_close($conn[0]);
|
mysqli_close($conn[0]);
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$conn[0]->close();
|
$conn[0]->close();
|
||||||
break;
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
pg_close($conn[0]);
|
pg_close($conn[0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +181,10 @@ function dbw_close($conn){
|
|||||||
|
|
||||||
function dbw_fetch_assoc($conn,$result){return dbw_fetch_array($conn,$result,"ASSOC");}
|
function dbw_fetch_assoc($conn,$result){return dbw_fetch_array($conn,$result,"ASSOC");}
|
||||||
function dbw_fetch_row($conn,$result){return dbw_fetch_array($conn,$result,"NUM");}
|
function dbw_fetch_row($conn,$result){return dbw_fetch_array($conn,$result,"NUM");}
|
||||||
|
|
||||||
|
function dbw_data_seek($conn,$result){return dbw_query_goto($conn,$result,$row = 0);}
|
||||||
|
function dbw_result_seek($conn,$result){return dbw_query_goto($conn,$result,$row = 0);}
|
||||||
|
|
||||||
function dbw_insert_id($conn){return dbw_last_id($conn);}
|
function dbw_insert_id($conn){return dbw_last_id($conn);}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user