MySQL Conversion Class
Version: 1.1
Last update: 3 year ago
Platforms: MySQL, PHP, XML, JSON
Example included: yes
Asq a question Purchase
description

MySQL Conversion Class allows you:

  • easy way to Import/Export MySQL Data;
  • easy way to create API;
  • get result of MySQL query as XML, JSON or associative array;
  • print result (with headers) of MySQL query as XML, JSON or associative array;
  • insert, update or delete data on MySQL table using XML, JSON or associative array;
  • support MySQL and MySQLi driver;
  • the JSON created can be indented or not, you choose;
  • easy to get class-failure errors;
  • examples of both functions are included.
step 1

Function DB::init($settings) can get parameters:

  • $settings['driver'] : MySQL Driver 'mysql' or 'mysqli' value. From PHP5.5 'mysql' not provided, please use 'mysqli';
  • $params['host'] : The MySQL server. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost;
  • $params['username'] : The username;
  • $params['password'] : The password;
  • $params['database'] : The name of the database that is to be selected.
Example
$params = array( 'driver' => 'mysql', 'host' => 'localhost', 'username' => 'root', 'password' => 'pass123', 'database' => 'test' );
step 2

Function DB::init() return DB instance. And you can use DB functions to Get/Insert data.

GET DATA
  • query($query): Initiate select data from MySQL.
INSERT or UPDATE DATA
  • from_json($json): Initiate insert data to MySQL from JSON string;
  • from_xml($xml): Initiate insert data to MySQL from XML string;
  • from_array($array): Initiate insert data to MySQL from associative array.
DELETE DATA
  • delete($table,$where): Delete data from MySQL table, return true or false value.
step 3

Last step to get or Insert Data.

GET DATA
  • as_json($print): Return JSON string or print JSON data;
  • as_xml($print): Return XML string or print XML data;
  • as_array(): Return associative array.
INSERT DATA
  • insert($table): Insert data to MySQL table, return true or false value.
UPDATE DATA
  • update($table,$where): Update data on MySQL table, return true or false value.
get data
Print/Return as JSON
DB::init($params)->query($query)->as_json(true);
$json = DB::init($params)->query($query)->as_json();
Print/Return as XML
DB::init($params)->query($query)->as_xml(true);
$xml = DB::init($params)->query($query)->as_xml();
Return associative array
$result = DB::init($params)->query($query)->as_array();
insert or update data
Insert or update JSON data
DB::init($params)->from_json($data)->insert('test');
DB::init($params)->from_json($data)->update('test','id = "1"');
Insert or update XML data
DB::init($params)->from_xml($data)->insert('test');
DB::init($params)->from_xml($data)->update('test','id = "1"');
Insert or update associative array
DB::init($params)->from_array($data)->insert('test');
DB::init($params)->from_array($data)->update('test','id = "1"');
delete data
Delete data
DB::init($params)->delete('test','id = "1"');
Changelog
Version 1.1
  • Add method update($table,$where)
  • Add method delete($table,$where)
Version 1.0
  • Released
0