Basic Concepts
A Wee Tiny Blog is intended to be as easy to use as possible. In keeping with this idea, there are three basic concepts to keep in mind about the class. Firstly, it needs to be connected to the MySQL database in order to do anything. Secondly, blog entries are typically returned as
arrays with specific fields. Lastly, error handling is done using a flag within the class instead of exceptions. These are expounded on below.
Connecting to the Database
In order to use a database, you'll need to connect to both MySQL and the database in some manner. A Wee Tiny Blog currently only supports using MySQLi for this, but other ways will probably be added in the future.
To connect the $AWeeTinyBlog object to your database, you can use the
ConnectToDatabase method like so:
if (!$AWeeTinyBlog->ConnectToDatabase( 'localhost', 'username', 'password', 'database' )) {
//Handle the error here
echo 'Unable to connect';
exit();
}
Alternatively, if you want to handle the connection yourself, you can do so using
mysqli_connection and
mysqli_select_db. After calling these functions, pass the resource returned by
mysqli_connect to
UseConnection like so:
$connection = mysqli_connect( 'localhost', 'username', 'password' );
if (!$connection) {
//Handle the error here
echo 'Unable to connect';
exit();
}
$handle = mysqli_select_db($connection, 'database');
if (!$handle) {
//Handle the error here
echo 'Unable to select database';
exit();
}
//Attach A Wee Tiny Blog to the database
$AWeeTinyBlog->UseConnection($connection);
Handling the connection this way is the hard way, and it's really only recommended if you plan on doing something more involved like passing your own queries to the database.
Retrieving Entries
Many of the methods provided by $AWeeTinyBlog are ways to fetch entries from your database. Most of these return a single array with specific keys. The contents of these arrays can simply be echoed to the page.
$entry = $AWeeTinyBlog->GetEntry( 113 );
if (empty($entry)) {
//handle the error
echo 'Unable to retrieve post';
exit();
}
echo $entry['id'] . '<br>';
echo date( 'F jS, Y', $entry['date'] ) . '<br>';
echo $entry['subject'] . '<br>';
echo $entry['icon'] . '<br>';
echo $entry['content'] . '<br>';
Other methods return an array of arrays. These are lists of entries, so you'll want to use
foreach to walk through them.
$entries = $AWeeTinyBlog->GetPageOfEntries( 0, 10, true, false );
if (empty($entries)) {
//handle the error
echo 'Unable to retrieve post';
exit();
}
foreach( $entries as $entry ) {
echo $entry['id'] . '<br>';
echo date( 'F jS, Y', $entry['date'] ) . '<br>';
echo $entry['subject'] . '<br>';
echo $entry['icon'] . '<br>';
echo $entry['content'] . '<br><br><hr>';
}
Handling Errors
None of the methods in the AWeeTinyBlog class use exceptions to signal an error. This is partially my own preference, as I find exceptions to be an awkward way of handling errors, but it's also because PHP might be configured to display error messages when exceptions are encountered. These messages expose quite a bit about how your code works, and that's a security risk.
Instead, the AWeeTinyBlog class has two ways to alert you to an error condition. Firstly, each method returns either a boolean value, an integer or an array. In each case, these methods return specific values if an error is encountered ( FALSE, -1 or an empty array respectively ). The second way you can check about errors is to call
GetLastError, which returns the current value of the internal error flag. This flag will always be AWEETINYBLOG_ERROR_FREE unless something went wrong. In that case it will clearly indicate what the problem was.
Regardless of the error, you have control over what happens next. If you want to continue anyway, call
ClearError to reset the error flag before you do anything else.
$entry = $AWeeTinyBlog->GetEntry( 'This is not supposed to be a string!' );
if (empty($entry)) {
//This will print "Check parameters: something unexpected was encountered"
echo $AWeeTinyBlog->GetLastError();
exit();
}