PHP Classes

File: samples/mysql_gettable.php

Recommend this page to a friend!
  Classes of Aleksey Nemiro   Nemiro Data PHP   samples/mysql_gettable.php   Download  
File: samples/mysql_gettable.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Nemiro Data PHP
Compose and execute MySQL or PostgreSQL queries
Author: By
Last change:
Date: 8 years ago
Size: 1,204 bytes
 

Contents

Class file image Download
<?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 as MySql;
# import command class
use Nemiro\Data\DBCommand as 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)
        {
            echo
'<tr>';
            foreach (
$row as $field)
            {
                echo
'<td>'.(isset($field) ? $field : 'NULL').'</td>';
            }
            echo
'</tr>';
        }
        echo
'</tbody>';
        echo
'</table>';
    }
    else
    {
        echo
'The query returned no results...';
    }
}
catch (
Exception $ex)
{
    echo
'Error: '.$ex->getMessage();
}
?>