Quote:
Originally Posted by Salathe
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(true, true, $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
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.