<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import client for MySql use Nemiro\Data\MySql; # import command class use Nemiro\Data\DBCommand; # import parameters type list use Nemiro\Data\DBParameterType; # best practice is to use 'try { } catch { }' blocks try { # create a new client instance $client = new MySql(); # create commands $firtCommand = new DBCommand('SELECT * FROM users LIMIT @from, @to'); $firtCommand->Parameters->Add('@to', 20, DBParameterType::Integer); $firtCommand->Parameters->Add('@from', 10, DBParameterType::Integer); $secondCommand = new DBCommand('SELECT m.* FROM messages AS m ' . 'INNER JOIN (SELECT id_users FROM users LIMIT @users_from, @users_to) AS u ' . 'ON m.id_users = u.id_users ' . 'LIMIT @messages_limit'); $secondCommand->Parameters->Add('@messages_limit', 50, DBParameterType::Integer); $secondCommand->Parameters->Add('@users_from', $firtCommand->Parameters['@from']->Value, DBParameterType::Integer); $secondCommand->Parameters->Add('@users_to', $firtCommand->Parameters['@to']->Value, DBParameterType::Integer); $thirdCommand = 'SELECT * FROM stat'; # set commands to client instance $client->Command = array($firtCommand, $secondCommand, $thirdCommand); # execute the command and get DataSet $data = $client->GetData(); if (count($data) > 0) { # read tables
<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import MySql client use Nemiro\Data\MySql; # import command class use Nemiro\Data\DBCommand; # import parameters type list use Nemiro\Data\DBParameterType; # best practice is to use 'try { } catch { }' blocks try { # create client instance $client = new MySql(); # you can specify the text of the query into the Command property $client->Command = 'SELECT * FROM users'; # and execute the query by any method: # $client->GetRow(); # return single row # $client->GetTable(); # return all rows # $client->GetData(); # return array tables # for example, get all rows $table = $client->GetTable(); var_dump($table); # you can build a query from the input parameters, # but you will need to check the type and value of incoming data # it is bad practice $client->Command = 'DELETE FROM users WHERE id_users = ' . (int) $_GET['id']; # it is best to use parameterized queries $client->Command = new DBCommand('DELETE FROM users WHERE id_users = @id_users');
<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import client for MySql use Nemiro\Data\MySql; # import command class use Nemiro\Data\DBCommand; # best practice is to use 'try { } catch { }' blocks try { # create client instance $client = new MySql(); # create command $client->Command = new DBCommand('SELECT * FROM users LIMIT @limit'); $client->Command->Parameters->Add('@limit', 10); # execute the command and get table $table = $client->GetTable(); if (count($table) > 0) { # output columns of the table $columns = array_keys($table[0]); echo '<table>'; echo '<thead><tr>'; foreach ($columns as $column) { echo '<td>' . $column . '</td>'; } echo '</tr></thead>'; # output the table rows echo '<tbody>'; foreach ($table as $row) {
<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import MySql client use Nemiro\Data\MySql; # import command class use Nemiro\Data\DBCommand; # best practice is to use 'try { } catch { }' blocks try { # create client instance $client = new MySql(); # create a new command with parameters $client->Command = new DBCommand('INSERT INTO users (username, date_created) VALUES (@username, @date_created)'); # @username and @date_created is parameters name, # you can add a values for this parameters $client->Command->Parameters->Add('@date_created')->SetValue(date('Y-m-d H-i-s')); $client->Command->Parameters->Add('@username')->SetValue('anyname'); # execute the command $newId = $client->ExecuteScalar(); echo 'New row added. ID = ' . $newId . '<br />'; # you can change the parameters and execute the command again $client->Command->Parameters['@username']->SetValue('newValue'); $newId = $client->ExecuteScalar(); echo 'New row added. ID = ' . $newId . '<br />'; # and again $client->Command->Parameters['@username']->SetValue('123'); $client->Command->Parameters['@date_created']->SetValue(date('Y-m-d H-i-s')); $newId = $client->ExecuteScalar();
<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import client for MySql use Nemiro\Data\MySql; # import command class use Nemiro\Data\DBCommand; # best practice is to use 'try { } catch { }' blocks try { # create client instance $client = new MySql(); # create command $client->Command = new DBCommand('SELECT * FROM users LIMIT 1'); # execute the command and get single row $row = $client->GetRow(); if ($row != NULL) { # get filed name list of the row $fieldsName = array_keys($row); echo '<table style="width:500px;">'; echo '<tbody>'; # output data of the row foreach ($fieldsName as $field) { echo '<tr>'; echo '<td>' . $field . '</td>'; echo '<td><input type="text" value="' . $row[$field] . '" style="width:100%" /></td>'; echo '</tr>'; } echo '</tbody>';