Last active
July 18, 2017 10:29
-
-
Save scamba/342765f93474b768652185bc9f132e9b to your computer and use it in GitHub Desktop.
Basic PHP functions for connecting to database using PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
-- =================================================================================================== | |
-- Author: Sergio Camba (https://github.com/scamba) | |
-- Create date: 17/04/2017 | |
-- Update date: 15/07/2017 | |
-- Description: Function library for connection to database using PDO and compatible | |
with a multitude of databases such as MySQL, PostgreSQL, SQLite, Cubrid, ODBC, | |
MS SQL Server, Oracle, Firebird / Interbase, DB2, 4D, IBM and Informix. | |
-- Warning: To work properly, the PDO driver connection must be enabled on server, | |
+info: https://secure.php.net/manual/en/pdo.installation.php | |
-- =================================================================================================== | |
------------------------------ | |
--> Explanation of functions: | |
------------------------------ | |
* startConection($type, $host, $dbname, $user, $pass) | |
---------------------------------------------------------> | |
Description: | |
- Create a PDO object connection. | |
Params: | |
- $type: (string) Database type (mysql, postgre, etc). | |
- $host: (string) IP or host (usually localhost). | |
- $dbname: (string) Database name. | |
- $user: (string) Database user name. | |
- $pass: (string) Database user password. | |
Returns: | |
- A PDO object connection. | |
* SQLToArray($conn, $query) | |
-------------------------------> | |
Description: | |
- Get a SQL Select query, and returns an array with results. | |
Params: | |
- $conn: Object connection (created previously with startConnection). | |
- $query: SQL query type SELECT. | |
Returns: | |
- An array with results from SQL Select query. | |
* SQLExecute($conn, $query) | |
-------------------------------> | |
Description: | |
- Get a SQL query and execute it. | |
Params: | |
- $conn: Object connection (created previously with startConnection). | |
- $query: SQL query of any type, just execute it (essentially designed for INSERT, UPDATE, etc...). | |
Returns: | |
- true/false depending on whether the SQL statement is executed correctly or not. | |
* SQLToField($conn, $query) | |
-------------------------------> | |
Description: | |
- Get an SQL statement, execute and return a single data. | |
Params: | |
- $conn: Object connection (created previously with startConnection). | |
- $query: SQL statement type SELECT. | |
Returns: | |
- Single data from SQL statement. | |
* closeConnection() | |
------------------------> | |
Description: | |
- Close connection created with startConnection(...). | |
No get params. | |
Returns: | |
- Directly a null, which will be assigned to the connection variable created with startConnection(...). | |
*/ | |
function startConnection($type, $host, $dbname, $user, $pass) | |
{ | |
return new PDO( $type. ':host=' .$host. ';dbname=' .$dbname. ';charset=utf8', $user, $pass); | |
} | |
function SQLToArray($conn, $query) | |
{ | |
$aux = $conn->query($query); | |
return $aux->fetchAll(); | |
} | |
function SQLExecute($conn, $query) | |
{ | |
$aux = $conn->prepare($query); | |
return $aux->execute(); | |
} | |
function SQLToField($conn, $query) | |
{ | |
$aux = $conn->query($query); | |
return $aux->fetchColumn(); | |
} | |
function closeConnection() | |
{ | |
return null; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment