Download the PHP package aslamhus/db without Composer
On this page you can find all versions of the php package aslamhus/db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package db
DB
A Database class for mysqli prepared statements. Connect to your database and easily prepare statements, bind variables and execute queries. Currently Has methods for MYSQL select, insert, update and delete.
Install
Dependencies
- Composer
- composer package "vlucas/phpdotenv"
- MYSQL database with access privileges
- PHP
Getting started
Configuring your database
DB configures your database settings using environment variables. To set those up you'll need to install vlucas/phpdotenv
and create a .env file with your database name, host, username and password.
1. Install
If you have cloned the repo, install all dependencies.
If you are installing @aslamhus/db
as a package,
simply run
You should now have vlucas/phpdotenv
in your vendor directory.
More information on vlucas/dotenv
2. Create .env file in your project root directory with the following variables
3. Load your environment variables
create a config.php file with the following:
Make sure you specify your path to vendor/autload and your .env file
4. Connect to the database
Require your config.php file and then instantiate the DB class.
That's it! You should now be connected to your database. You can begin making queries.
Optional: Allowlist for table names and columns
If you would like to add an extra layer of security, you can create an allow list of valid table names and valid column names in your mysql queries.
Create two text files in the src directory, '.validcolumns' and '.validtables'
Separate names of valid tables and valid columns by line breaks.
DB
will then only perform queries on valid columns or tables.
If the files are left blank or do not exist, all tables and all columns will be allowed.
Testing
To see DB
in action you can run test/index.php
in a local environment with mysql and php installed.
Before running these examples you'll need to set up a test database.
- Login to your local mysql with admin privileges
- create a test database/test table by executing test/db/schema.sql
- add values to your test database by executing seeds.sql
Now you can run test/index.php in your local environment.
Examples
select
select
takes two parameters with an optional third. The first parameter is the columns to select, the second is the tablename and the third is an optional array of conditions.
Select everything
Select with conditions
In this example we select everything from the table 'users' where a the username has a value of the variable $username.
... more examples to come!
Author
aslamhus