• This repository has been archived on 28/Mar/2020
  • Stars
    star
    103
  • Rank 332,086 (Top 7 %)
  • Language
    PHP
  • Created almost 12 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍎 Simple PHP class for doing standard MySQL actions, such as select, insert, update and delete rows.

Database.php

Build Status codecov

Database.php is a simple PHP class for doing standard MySQL actions, such as selecting, inserting, updating and deleting database rows. It also includes some nice functionality, like auto-escaping to protect your database from malicious code and automatic serializing of arrays.

Usage

Initiating

Initiate a database connection using by creating a new Database() object.

require_once('Database.php');

$db = new Database($database_name, $username, $password, $host); // $host is optional and defaults to 'localhost'

Select

Select rows from a database table

Usage:

$db->select($table, $where, $limit, $order, $where_mode, $select_fields)

Arguments:

  • string $table - name of the table to select from
  • array/string $where - array or string holding the filters/'WHERE' clause for the query
  • int/string $limit - integer or string holding the 'LIMIT' clause
  • string $order - string holding the 'ORDER BY' clause
  • string $where_mode - whether to add an 'AND' or 'OR' after each item in the $where array, defaults to AND
  • string $select_fields - the fields to select (SELECT <$select_fields> FROM ...), defaults to *

Example:

// get the first 10 candy bars that are sweet, and order them by amount
$db->select('candy', ['sweet' => 1, 'spicy' => 0], 10, 'amount DESC');
// get the ids 1, 2,5,9  from products
$db->select('products', ['id' => 'in (1,2,5,9)'], false, false,'OR');

Reading results

Reading the results can be done with the following functions:

  • $db->count() returns the number of selected rows, equal to mysql_num_rows()

  • $db->result() returns all matches rows as an array containing row objects

  • $db->row() returns the first row that matches the query as an object

  • $db->result_array() returns all matches rows as an array containing row arrays

  • $db->row_array() returns the first row that matches the query as an array

Please note that you can call any of these functions also directly after the $db->select() call, like shown below:

echo $db->select('candy', ['sweet' => 1], 10)->count();

There are a few other methods available for queries that might come in handy:

  • $db->sql() returns the sql query that was last executed

Insert

Insert data into a database table

Usage:

$db->insert($table, $fields=[])

Example:

$db->insert(
	'candy', [
		'name' => 'Kitkat original',
		'sweet' => 1,
		'spicey' => 0,
		'brand' => 'Kitkat',
		'amount_per_pack' => 4
	]
);

Tip! You can call $db->id() immeadiately after a $db->insert() call to get the ID of the last inserted row.

Update

Update one or more rows of a database table

Usage:

$db->update($table, $fields=[], $where=[])

Example:

// set amount per pack to 5 for all Kitkats
$db->update(
	'candy', [
		// fields to be updated
		'amount_per_pack' => 5
	], [
		// 'WHERE' clause
		'brand' => 'Kitkat'
	]
);

Delete

Remove one or more rows from a database table

Usage:

$db->delete($table, $where=[])

Example:

// delete all Kitkat candy
$db->delete(
	'candy', [
		// 'WHERE' clause
		'brand' => 'Kitkat'
	]
);

Singleton

Access the database instance outside the global scope after initializing it

Usage:

$my_db = Database::instance();

Example:

// Global scope
$db = new Database($database_name, $username, $password, $host);

// Function scope
function something() {
    // We could simply use `global $db;`, but using globals is bad. Instead we can do this:
    $db = Database::instance();

    // And now we have access to $db inside the function
}

More Repositories

1

php-pkpass

💳 PHP class for creating passes for Wallet on iOS.
PHP
908
star
2

react-native-map-link

🗺 Open the map app of the user's choice.
TypeScript
651
star
3

react-native-email-link

📭 Open an email client from React Native (for 'magic link' type functionality).
JavaScript
390
star
4

react-native-check-version

🥕 Get the latest version of your app.
JavaScript
188
star
5

lambda-sample-events

⚡️ A library of example event payloads for AWS Lambda.
JavaScript
48
star
6

react-native-keycode

🔢 Show the user a input form for a fixed-length code or password.
JavaScript
43
star
7

macaw

🦜 Scalable email templates for Node.js applications.
JavaScript
26
star
8

mapkit-jwt

🗺 Simple class to create MapKit and MusicKit JWT tokens.
PHP
18
star
9

react-native-confetti-view

JavaScript
9
star
10

things-stats

📊 Stats tool for Things.
JavaScript
6
star
11

csv-hero

🦸‍ The easiest tool to merge, filter and mangle CSV and JSON files.
JavaScript
5
star
12

jsnotebook

📘 Play around with Javascript in your browser and share notebooks.
JavaScript
5
star
13

todo-chrome-extensions

📩 Chrome extension to quickly add Gmail conversations as tasks in Things & OmniFocus.
JavaScript
5
star
14

tschoffelen

🌎 My personal website.
HTML
4
star
15

react-update-popup

🔄 Ask users to refresh when there is a new version of the app available.
TypeScript
4
star
16

things-gmail

📩 A Chrome extension to quickly add Gmail conversations as a task in Things.
JavaScript
3
star
17

serverless-starter

Starter boilerplate for a serverless API.
JavaScript
2
star
18

breakout-team

📹 Video breakout rooms made easy.
JavaScript
1
star
19

mirri

🌿 A little file sharing app.
JavaScript
1
star
20

gatsby-remark-fountain

Plugin to render Fountain screenwriting syntax in `gatsby-transformer-remark`.
JavaScript
1
star
21

advent-2020

🎅 My Advent of Code solutions.
JavaScript
1
star
22

alfred-schof.link

Alfred workflow to copy clipboard to schof.link.
JavaScript
1
star