PHP code example of jabarihunt / mysql

1. Go to this page and download the library: Download jabarihunt/mysql library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jabarihunt / mysql example snippets




 use jabarihunt/MySQL;

use jabarihunt/MySQL as DB;

/*
 * QUERY THE DATABASE WITH A PREPARED STATEMENT (RECOMMENDED)
 *
 * prepare($query, $paramValues, $paramTypeString = NULL):
 * CONVENIENCE METHOD THAT RETURNS ARRAY OF DATA FOR QUERIES THAT RETURN A RESULT SET, THE NUMBER OF AFFECTED ROWS FOR ALL
 * OTHER QUERIES, OR FALSE ON ERROR.  $paramTypeString IS OPTIONAL, ALL VALUES WILL BE SENT AS STRINGS IF NOT PROVIDED.
 */

    $data = DB::prepare('SELECT name, email FROM users WHERE age > ? AND status = ? AND days_active >= ?', [357, 'retired', 30], 'isi');

/*
 * QUERY THE DATABASE AS A STANDARD QUERY
 *
 * query($query):
 * CONVENIENCE METHOD THAT RETURNS AN ARRAY OF DATA FOR QUERIES THAT RETURN A RESULT SET, TRUE ON SUCCESS, OR FALSE ON ERROR.
 */

    $data = DB::query("SELECT name, email FROM users WHERE age > 69 AND status = 'retired' AND days_active >= 30");

/*
 * SANITIZE VALUES WHEN NOT USING PREPARED STATEMENTS
 * THIS METHOD USES PHP filter_var() SANITIZATION BASED ON THE DATA TYPE
 */

    $string = 'Am I a good string or a naughty string?';

    $string = DB::sanitize($string, DB::DATA_TYPE_INTEGER);     // All characters removed since sanitizing as an int data type!
    $string = DB::sanitize($string);                            // String remains since method defaults to DB::DATA_TYPE_TEXT
    $string = DB::sanitize(NULL);                               // Null values are converted to an empty string before sanitizing

    // CLASS DATA TYPES ARE BASED ON MYSQL DATA TYPES.  DATA TYPES ARE DECLARED IN THE CLASS AS SHOWN BELOW:

        const DATA_TYPE_INTEGER  = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'bit'];
        const DATA_TYPE_REAL     = ['float', 'double', 'decimal'];
        const DATA_TYPE_TEXT     = ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext'];
        const DATA_TYPE_BINARY   = ['binary', 'varbinary', 'blob', 'tinyblob', 'mediumblob', 'longblob'];
        const DATA_TYPE_TEMPORAL = ['date', 'time', 'year', 'datetime', 'timestamp'];
        const DATA_TYPE_SPATIAL  = ['point', 'linestring', 'polygon', 'geometry', 'multipoint', 'multilinestring', 'multipolygon', 'geometrycollection'];
        const DATA_TYPE_OTHER    = ['enum', 'set'];

/*
 * GET MySQLi OBJECT
 */

    $mysqlObject = DB::getMySQLObject();

/*
 * MYSQL DUMP -> CONVENIENCE METHOD FOR DUMPING A DATABASE SOMEWHERE ON THE MACHINE | USE WITH CAUTION!!!
 * CREATES MYSQL DUMP FILE AT GIVEN PATH WITH FORMAT: <database>_<year>-<month>-<day>_<unix timestamp>.sql
 */

    DB::backup('path/to/my/backup/folder');