Class GxDBConnect
License: MIT
Author: Leandro Silva
Example:
This sample includes tight security.
Use value binding, a whitelist, and checker methods if you plan to construct your SQL statements with values coming from a form (or other user inputted method). Using these featured will help to prevent SQL attacks.
include "./GxDBConnect.class.php"; include "./DSNHelper.class.php"; try { $conn = new GxDBConnect(DSNHelper::mysql("my_database"), "my_username", "my_pass"); $conn->col_whitelist = array("first", "last"); $conn->tbl_whitelist = array("names_table"); $f_name = $_GET["first_name"]; $l_name = $_GET["last_name"]; $table = $_GET["table_name"]; $data = $conn->query(" SELECT {$conn->col_check($f_name)} FROM {$conn->tbl_check($table)} WHERE {$conn->col_check($l_name)} = :ln ", array( $conn->bind_value(":ln", "Doe") ), PDO::FETCH_NUM ); print_r($data); } catch(GxDBException $e) { exit($e); }
This sample includes a more simple use case.
include "./GxDBConnect.class.php"; try { $conn = new GxDBConnect("mysql:host=localhost;dbname=my_database", "my_username", "my_pass"); $data = $conn->query(" SELECT first FROM names_table WHERE last = 'Doe' "); print_r($data); } catch(GxDBException $e) { exit($e); }
Classdesc:
Securely execute commands on a database using PHP Data Objects — many security features added. Note that methods prepended with 'run_' execute specific statements; use the 'query' method for custom queries.
Located at GxDBConnect.class.php
Methods summary
public
object
|
#
__construct( string $dsn, string $usr = "root", string $pw = "root", array $opts = null )
Constructor. By default, the PDO attribute ATTR_EMULATE_PREPARES is set to false and ATTR_ERRMODE is set to ERRMODE_EXCEPTION. |
public
|
#
blacklist_add( string $str )
Adds a value to your blacklist filter. Before any query is run, your statement will be checked for any blacklisted strings. If a blacklisted string is found, the query will not be executed and a GxDBException exception will be thrown. By default, the blacklist filter contains the following: ["DROP", "DELETE", "--", "/*", "xp_", ";"] |
public
|
|
public
array
|
|
public
|
|
public
string
|
|
public
string
|
|
public
array
|
#
bind_value( mixed $parameter, mixed $value, integer $data_type = null )
To be used as the bind argument in the 'query' method. Works like PDO's 'bindValue' method. |
public
array
|
|
public
|
|
public
boolean
|
|
public
integer
|
|
public
array
|
|
public
array
|
|
public
boolean
|
|
public
integer
|
|
public
array|null
|
|
public
boolean
|
#
run_export( string $tbl, boolean $pretty_print = false, string $relative_dir = "" )
Exports your table as a JSON formatted file. |
public
|
#
run_tbl_to_html( string $stmt, integer $paginate_at = 0, string $pg_query_name = "pg", boolean $use_default_styles = true )
Echos an HTML table with your data. |
Properties summary
public
object
|
$conn
The PDO connection object. |
|
public
array
|
$col_whitelist
A whitelist of columns that can be queried. Use in concert with the 'col_check' method. |
|
public
array
|
$tbl_whitelist
A whitelist of tables that can be queried. Use in concert with the 'tbl_check' method. |
|
public
string
|
$get_last_stmt
The statement you last queried. |
|
public static
string
|
$version
The release version. |
#
"3.0.0"
|
public static
string
|
$echo_uncaught_errors
Set to true to output uncaught errors. Defaults to false for better security. |
#
false
|