apsxj/mysql

MySQL Library for APSXJ Server

v0.1.0 2021-05-10 00:35 UTC

This package is auto-updated.

Last update: 2024-09-10 08:10:46 UTC


README

MySQL Library for APSXJ Server

Getting Started

  1. Add the following to your composer.json file:
  "require": {
    "apsxj/mysql": "dev-main"
  },
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/apsxj/mysql.git"
    }
  ]
  1. Run composer install

  2. Before calling any of the methods, require the vendor autoloader

// For example, from the root directory...
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
  1. Extend the Connection class
<?php

class Connection extends \apsxj\mysql\Connection 
{
  public function __construct()
  {
    parent::__construct(
      'localhost',
      'test_db',
      'test_user',
      'supersecret'
    );
  }
}

$connection = new Connection();

$rows = $connection->fetch("SELECT * FROM `test_table`;");
  1. Extend the Table class
<?php

use apsxj\mysql\Column;
use apsxj\mysql\Type;

class Table extends \apsxj\mysql\Table
{
  public function __construct()
  {
    parent::__construct(
      new Connection(),
      'test',
      array(
        new Column('id', Type::INT),
        new Column('created', Type::INT),
        new Column('updated', Type::INT),
        new Column('deleted', Type::BOOL),
        new Column('name')
      )
    );
  }

  public function insert($name)
  {
    $sql = "INSERT INTO `test` (`id`, `created`, `updated`, `deleted`, `name`) VALUES (NULL, ?, ?, 0, ?);";
    $date = date('U');
    $args = array($date, $date, $name);
    return $this->connection->insert($sql, $args);
  }
}

$table = new Table();

$table->insert('Test Name');