PHP: basic MySQL database class

Before digging into the details of PHP design patterns (including the Singleton pattern), I used to define my database classes as abstract to avoid instantiation. This is a rough solution and it's not really suitable for a MVC environment. However, in this post I'm going to show you a really basic implementation of a basic class that uses a MySQL database.

jQuery: formatting MySQL blog dates

In this post I'm going to show you how to format a MySQL blog date into another format. Generally speaking, this kind of formatting is performed before sending the output to a browser by the CMS itself, so this post has only a demonstrative purpose. MySQL has the following format, called DATETIME:

yyyy-mm-dd hh:mm:ss

We want to turn the above format into this:


<span class="day">dd</span>
<span class="month">mm</span>
<span class="year">yyyy</span>

Let's get started with a basic HTML structure like this:

<div class="post">
 
 <div class="post-title">
  
  <h2>Lorem ipsum dolor</h2>
  
  <p>2010-12-28 17:27:28</p>
  
 </div>
 
</div>

As you can see, the date contained within the paragraph is in the MySQL format. Now we can add jQuery:

$(document).ready(function() {
 
 
 $('div.post-title').each(function() {
  
  var title = $(this);
  var date = title.find('p').text();
  
  // MySQL date: yyyy-mm-dd hh:mm:ss
  
  var rawDate = date.replace(/-+/g, ' ').replace(/:+/g, ' ');
  
  var dateArr = rawDate.split(' ');
  
  var reducedArr = dateArr.slice(0, 3);
  
  var day = reducedArr[2];
  var month = reducedArr[1];
  var year = reducedArr[0];
  
  var html = '<span class="day">' + day + '</span>' +
             '<span class="month">' + month + '</span>' +
       '<span class="year">' + year + '</span>';
       
        title.find('p').html(html);  
 });
 
});

We replace all the '-' and ':' tokens of the paragraph's text with spaces. Then we use those spaces to create an array and we reduce it using the slice() method. So we have all the textual parts we need. Finally, we create the HTML fragment to be inserted in the paragraph itself. You can see the final result here.

jQuery: retrieving data from a database

Really interesting video that shows you how to retrieve data from a MySQL database using PHP as back-end language and jQuery's AJAX methods on the client side. Some things are not correct if you want to achieve the result of a fully validated document, such as the name attribute used on the search form, but these things can be easily fixed. Really valuable if we compare this video to the paramount amount of inconsistent results that you get from YouTube.

User-friendly URLs in user profiles

One of the worst things in a web application is the fact of using URLs such as user.php?id=1234 for displaying user profiles. For example, a web application such as Facebook wisely doesn't follow this approach. Instead, it uses something like directory/username for user profiles. This also allows the newly created page to be better indexed by search engine.

I don't have access (of course I don't!) to Facebook's database structure, but I guess that there should be a table which stores my credentials, such as first name and last name (say user_first_name and user_last_name), so the point is creating an unique URL by using these credentials, something like this:

$query = mysql_query("SELECT user_first_name, user_last_name FROM user WHERE user_id='$uid'");
$results = mysql_fetch_array($query);
 $first_name = $results['user_first_name'];
 $last_name = $results['user_last_name'];

$first_name = strtolower($first_name);
$last_name = strtolower($last_name);

$user_ url = $first_name . $last_name;

For the sake of brevity, I've omitted to remove any trailing white-space from both strings, but now you can get the idea. Obviously the variable $uid must be previously set. Anyway, by doing so, you have a good starting point for testing. A problem that will certainly arise is duplicate user credentials, for example when there are one or more users with the same first name or last name. In this case you can use other data from the table, such for example an ID or the username chosen during the registration process.

Two PHP functions to handle MySQL dates

MySQL dates (datetime type) usually follow this pattern: 2010-03-11 17:18:02. Here are two PHP functions to create and format MySQL dates on your site/blog:

function create_sql_date() 
{
           
  // MySQL datetime: YYYY-MM-DD hh:mm:ss
          
  $timestamp = time();
          
  return strftime('%Y-%m-%d %H:%M:%S', $timestamp); 
           
}
       
function format_sql_date($date) 
{
           
  $date_time = new DateTime($date);
         
  return ($date_time->format('F j, Y'));   
           
           
}

The first function is useful to create fresh dates to be inserted in the database, while the latter can be deployed to format MySQL dates after retrieving them from a query.