PHP code example of restful-template / sql-manager

1. Go to this page and download the library: Download restful-template/sql-manager 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/ */

    

restful-template / sql-manager example snippets



$dbInfo = [
	"host" => "localhost",
	"name" => "database_name",
	"charset" => "utf8",
	"user" => "database_user",
	"password" => "database_password",
	"prefix" => "db_",
	"tables" => [
		"cars" => [
			"prefix" => "car_",
			"primary" => "id",
			"unique" => [ "name" ],
			"fields" => [
				"id" => "int(11)",
				"model" => "varchar(30)",
				"brand" => "varchar(30)",
				"year" => "char(4)"
			]
		]
	]
];


use RESTfulTemplate\SQLManager as SMan;

$db = new SMan( $dbInfo );
$tableName = "cars";
$data = $db->select( $tableName );
// $data contains an array of database result rows


use RESTfulTemplate\SQLManager as SMan;

$db = new SMan( $dbInfo );
$tableName = "cars";
$data = $db->select( $tableName, [ "id, year" ] );
// $data contains the two requested columns (id and year) for each row in cars table

$data = $db->select( $tableName, [], [ "year" => "2010" ] );
// $data contains rows from cars table where column year has value 2010


use RESTfulTemplate\SQLManager as SMan;

$body = [
	"model" => "new model",
	"brand" => "some brand",
	"year" => "2020"
];

$db = new SMan( $dbInfo );
$tableName = "cars";
$result = $db->insert( $tableName, $body );
// $result is true if everything ok and false if some error happens.


use RESTfulTemplate\SQLManager as SMan;

$body = [
	"model" => "correct model",
];
$where = [ "id" => 1 ];

$db = new SMan( $dbInfo );
$tableName = "cars";
$result = $db->update( $tableName, $body, $where );
// $result is true if everything ok and false if some error happens.


use RESTfulTemplate\SQLManager as SMan;

$db = new SMan( $dbInfo );
$tableName = "cars";
$where = [ "id" => 1 ];
$result = $db->delete( $tableName, $where );
// $result is true if everything ok and false if some error happens.