Skip to content

Modularr/Database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Database

An easy to use class for Database queries in PHP.

API

Database::connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql');
Database::getPdo();
Database::setPdo($db);
Database::query($query, $params = array());
Database::fetchAll($query);
Database::fetchAll_safe($query);
Database::fetch_assoc($query);
Database::fetch_safe_assoc($query);
Database::fetch_object($query);
Database::fetch_safe_object($query);
Database::num_rows($query);

See Below for usage.

Connecting

Database::connect('database','password','username','host');

Note the reverse parameters. We do this because of the ommitable variables.

PDO Objects

If you want to use an existing PDO object to connect:

$db = new PDO; # Use Connection details
Database::setPdo($db); # Pass the object

If you want to use this database's PDO object to pass to other objects:

$db = Database::getPdo(); # Returns PDO object after connect() is called

Query

$query = Database::query("SELECT * FROM table WHERE id = ?", [$_GET['id']]);

This is a query with bind parameters. First argument is the statement, second argument is an array of parameters (optional)

Note: We passed the query into a variable for later re-use.

Fetch and Safe Fetch

This is regular returned object. You still need to apply htmlspecialchars yourself.

$table = Database::fetch_object($query);

This is safe returned object. htmlspecialchars is applied to all the objects's properties.

$table = Database::fetch_safe_object($query);

Num Rows

Database::num_rows($query); # Equivalent of $pdo->rowCount();

Data Examples

# Loop Objects
while($entry = Database::fetch_safe_object($query))
{
	# Because of fetch_safe_object we don't need to apply htmlspecialchars
    echo '<a href="page?id='.$entry->id.'">'.$entry->name.'</a><br />';
}
# Single Object
$entry = Database::fetch_safe_object($query);
echo $entry->name;

# Loop Objects Using Foreach instead with Fetchall
foreach(Database::fetchAll_safe($query) as $entry)
{
	# Because of fetchAll_safe we don't need to apply htmlspecialchars
    echo '<a href="page?id='.$entry->id.'">'.$entry->name.'</a><br />';
}
# Single Object
$entry = Database::fetchAll_safe($query);
echo $entry[0]->name;

Installation

via Composer:

{
    "require": {
        "modularr/database": "1.*"
    }
}

Then run:

composer update

Or install like so:

composer require modularr/database

make sure you have:

require 'vendor/autoload.php';

Manual:

  1. Download Release Or copy file manually
  2. Include Database.php (found under src/)
  3. Check out the example

Packages

No packages published

Languages