usernane / phMysql

PHP library for building MySQL database schema and quires.
MIT License
4 stars 2 forks source link
mysql php query-builder schema-builder

phMysql

Object oriented PHP library for building MySQL database schema and quires.

GitHub All Releases

API Docs

This library is a part of WebFiori Framework. To access API docs of the library, you can visid the following link: https://webfiori.com/docs/phMysql .

Features

Supported PHP Versions

The library support all versions starting from version 5.6 up to version 7.4.

Installation

The easy option is to download the latest release manually from Release.

The Idea

The overall idea of the library is as follows, every table in the database is represented as an instance of the class 'MySQLTable'. The instance is associated with an instance of the class 'MySQLQuery'. The main aim of the class 'MySQLQuery' is to construct different types of queries which can be executed and get data from the table.

The class 'MySQLink' is used to connect to MySQL database and execute any instance of the class 'MySQLQuery'. In addition to that, it is used to access the data which can be the result of executing a 'select' query.

Creating Database Tables

The first step in using the library is to create your database tables. As we have said before, every table is represented as an instance of the class MySQLTable. Also, we have said that an instance of this class is linked to the class MySQLQuery.

Let's assume that we want to create a database table with following structure:

The following code shows how to connect to the database. It also checks for connection errors.

use phMysql\MySQLLink;

$conn = new MySQLLink('localhost', 'root', '123456');

if($conn->getErrorCode() != 0) {
  //connection error. Show error message
  echo $conn->getErrorMessage();
} else {
  //connected. Select database now.

  if($conn->setDB('my_database')) {

    //connected. Now can execute quires.

  } else {
    //unable to set database
    echo $conn->getErrorMessage();
  }
}

Executing MySQL Query

After connecting to the database, we can start running queries on it. As we have said before, the class MySQLQuery is used to construct our queries. In order to execute them, we have to use the class MySQLLink. To be specific, the method MySQLLink::executeQuery(). The method will return a boolean. If the query is successfully executed, the method will return true. If it fails, the method will return false.

Lets assume that we have a connection to a database and we have our query class that has the table users_information. The following code sample shows how to execute an insert query.

$query->insertRecord([
  'user-id'=>99,
  'username'=>'MySuperHeroIsYou',
  'password'=>'f5d44b6d4a7d91821d602d03c096280e86888fa16cf9c27c540bbc2fd4e73932',
  'created-on'=>date('Y-m-d H:i:s')
]);
if($conn->executeQuery($query)) {
  //query executed without errors
} else {
  //something went wrong. Show error message
  echo $conn->getErrorMessage();
}

Fetching Raw Data

Mapping Query Result to Class Object

Joining Two Tables