Thread: The SQL Class
View Single Post
Old 01-17-2009, 11:17 PM   #64 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
So let me get this straight with an example. Basically we'll connect to the database and return some results:
PHP Code:
$db = new DBmysql;
$db->setHandler('localhost''root''mysupersecretpassword''phlox_dev')
   ->
connect()
   ->
select();

$sql  'SELECT * FROM sample'// Assume columns: id, title

// Get associative array of rows
$rows $db->getFetch(truetrue$sql);
// or $rows = $db->exeQuery($sql)->loadFetch(true)->getFetch();
// or $rwos = $db->loadQuery($sql)->exeQuery()->getFetch(true, true);
// or $rows = $db->loadFetch(true, $sql)->getFetch();

foreach ($rows as $row)
{
    
sprintf("Yay I got record ID: %d\n"$row['id']);

All of that was correct, indeed. Obviously, since you want to get the fetch directly, the code you didn't comment out is the preferred one(but you probably already figured that out!).

Quote:
Originally Posted by Ross View Post
Could you give an example? Sorry, just never been in that position before ;)
Sure.
In my previous projects, I had a factory, which managed all of the classes I wanted to import into it, not only the DBclass. Some of those classes, did not use any params in the construct, so I found it best to start all the classes first.

core.php
PHP Code:
include('classes/TANAXIA.php');

$db TANAXIA::getInstance('DBmysql');
$pagination TANAXIA::getInstance('Pagination');
// etc 
And then
classes.php
PHP Code:
include('core.php');

$db->setHandler($host$user$pass$data);
$pagination->setConfiguration($maxperpage); 
And finally:
config.php
PHP Code:
$host 'localhost';
$user 'root';
$pass 'secret';
$data 'testing';

$maxperpage 10;

include(
'classes.php'); 
In my index files, I would then only have to do this
PHP Code:
include('includes/config.php'); 
to have acccess to all my classes, which are managed from the classes.php file, and settings configurable from config.php.

If I want to add another class, I simply include it in my core file, set it up in my classes file(assuming it needs any functions runned before you can use it from index files), and have the settings stored in the config file.

A simple, but quite straight to the point example.
__________________

Last edited by Tanax : 01-20-2009 at 07:19 PM.
Tanax is offline  
Reply With Quote