Tag Archives: database

MySQL in OpenSencillo – Connecting and create new table

We have four classes for SQL in three layers:

MySQL classes diagram

  • mysql – Main SQL class with minimal servicing database (manual SQL query and testing only)
  • mysqlEdit – Extension for class mysql (useful for generating simple SQL queries)
  • logMan – Extension for class mysqlEdit (useful for login and user management)
  • mysqlInterface – Extension for class mysqlEdit (best way for create and management custom tables)

mysqlInterface PHP OpenSencillo example:

<?php
//yourcode.php
//call class mysqlInterface
$mysql = new mysqlInterface($DBHost,$DBName,$DBUser,$DBPass);
//prepare table structure
$newTable = array('hostCtr'=>array(
    'id'=>array('type'=>'int','auto_increment'=>true,'primary_key'=>true),
    'ip'=>array('type'=>'varchar(15)','unique_key'=>true),
    'datetime'=>array('type'=>'datetime'))
);

//create shadow configuration and open connection to OpenSencillo database
$mysql->config();
$mysql->connect();
//add new table
$mysql->dbCreateTable($newTable);
//collected all queries from the last execute and send queries to SQL server
$mysql->execute();
echo "SQL changes saved!";
?>

Output to SQL:
New table: hostCtr
Structure of new table:

  • id – int, auto_increment, primary_key
  • ip – varchar(15), unique_key
  • datetime – datetime

Now you can save your own data. Continue like this:

<?php
//continue yourcode.php
//assign data structure
$newInsert = array('hostCtr'=>array(
    'id'=>"''",
    'ip'=>"'".$_SERVER['REMOTE_ADDR']."'",
    'datetime'=>"'".date('Y-m-d H:i:s')."'")
);
//add structure and data to SQL generator
$mysql->insert($newInsert);
//execute generated queries
$mysql->execute();
?>

Now you save new id, new unique ip with date and time.

Facebooktwittergoogle_plusredditpinterestlinkedinmail