PHP Classes

File: samples/mysql_getrow.php

Recommend this page to a friend!
  Packages of Aleksey Nemiro   Nemiro Data PHP   samples/mysql_getrow.php   Download  
File: samples/mysql_getrow.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: 11 years ago
Size: 1,076 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 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>';
        echo '</table>';
    }
    else
    {
        echo 'The query returned no results...';
    }
}
catch (Exception $ex)
{
    echo 'Error: '.$ex->getMessage();
}
?>