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.

Categories
WordPress

Changing your WordPress Database Table Prefix

An article at BlogSecurity.net points us to a plug-in that will change the table-prefix of the MySQL database tables used by your blog.

This step contributes to the security of your blog by letting you swap the default (and thus obvious) 'wp_' table prefix for something obscure that a hacker could not guess at.

Remember that if your WP installation does not have error reporting turned off an attacker may yet be able to force your installation to reveal critical information by forcing an error and reading sensitive database information from the error messages.

Categories
PHP

Capturing the filename from a path in PHP

When you want to isolate the base filename from a path string, this is the way to do it:

[sourcecode language='php']
$path = "/home/project/folder/mypage.php";
$file = basename($path);          // $file is "mypage.php"
$file = basename($path, ".php");      // $file is "mypage"
[/sourcecode]

See dirname() and pathinfo() for related information.