Download the PHP package tschoffelen/db.php without Composer
On this page you can find all versions of the php package tschoffelen/db.php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tschoffelen/db.php
More information about tschoffelen/db.php
Files in tschoffelen/db.php
Package db.php
Short Description Simple PHP class for doing standard MySQL actions.
License MIT
Informations about the package db.php
Database.php
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.
Select
Select rows from a database table
Usage:
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 toAND
- string
$select_fields
- the fields to select (SELECT <$select_fields> FROM ...), defaults to*
Example:
Reading results
Reading the results can be done with the following functions:
-
$db->count()
returns the number of selected rows, equal tomysql_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:
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:
Example:
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:
Example:
Delete
Remove one or more rows from a database table
Usage:
Example:
Singleton
Access the database instance outside the global scope after initializing it
Usage:
Example: