Categories
WordPress

Understanding WordPress Themes

If you want to get into WordPress, and want to tweak the themes a little, you'll need do understand a little bit about how themes work.  These resources will help.

Categories
Database MySQL PHP

Time Stamp differences in MySQL and PHP

MySQL and PHP handle time and data data in different ways, and it's important to be aware of the difference.

Both these environments have a TIMESTAMP construct, but they're not entirely compatible.

While PHP uses a UNIX timestamp format (an integer representing the number of seconds since January 1st, 1970) MySQL's TIMESTAMP data type uses a YYYY-MM-DD HH:MM:SS format.

You can overcome this by using the mysql funtion UNIX_TIMESTAMP() to pull dates in the UNIX format native to PHP.

Making this conversion in your SQL query is generally more efficient and simple than converting in PHP.  Remember, you always want to work your data as much as you can in the SQL query and deliver a clean set of data to PHP.

In fact, if you like you can pull the same attribute in different ways in the same query, creating what I'll call pseudo-attributes, as in;

[SOURCECODE language='sql']SELECT UNIX_TIMESTAMP(date_attribute) AS unix_date,date_attribute AS nice_date FROM mytable;[/SOURCECODE]

This gives you two dates to work with in PHP, represented as unix_date and nice_date for a more human readable format, and balances the processing between your web and database servers.

Categories
MySQL

Conditional Queries in MySQL

Probably the first thing we learn in SQL (Structured Query Language) is a simple SELECT statement to draw information out of the database.

Let's assume we're working with a membership table (members) in our database that has first name (fname) and last name (lname) attributes.  We can draw the names out of the database like this:

1
SELECT fname,lname FROM members;

So far so good.  Now lets say we want to include informal names for those who use them, like 'Rob' for 'Robert' or 'Bill' for 'William', so we add another attribute, iname for the informal name.

However not everyone has an informal name, so we can't just change our query to reflect iname instead of fname.  We need to use a conditional query to select iname if it exists, otherwise fname. Here's how to do it;

1
SELECT IF(iname !='',iname,fname),lname FROM members;

In plain English (or pseudocode) we are saying "If there's a value for iname, use it, otherwise use the value in fname."

We can dress this up just a bit more with the concat() function, which we use to contactenate, or paste, strings together.

1
SELECT concat(IF(iname !='',iname,fname),' ',lname) AS "FULL Name" FROM members;

This will return the name as one attribute called "Full Name" using the informal name if available (otherwise the first name) followed by a space and the last name.

There are some important advantages to doing this in the database query rather than a server-side scripting language like PHP or ASP.  First, you're moving some of the processing work to the SQL server, which balances the load between servers.  Second, you're simplifying our server side scripting code by delivering it a clean set of data which can be used as it is.  The conditional clause in the SQL query is much simpler than the code that would be required to do the same thing in PHP.

It's generally a good idea to do as much of your conditional processing as possible in the SQL query so your PHP or other server-side script code will be easier to write, maintain, and understand.

Categories
PHP

PHP Forms Processing

I could write a long article on PHP forms processing, or I could send you to this very useful article.

Categories
WordPress

Securing your WordPress Blog

Securing a blog is essential.

Here are some resources presented to point you to further information on how to do this.