All posts by Dave

About Dave

Designing websites since 1998.

Using REGEXP in mySQL WHERE Conditionals

SQL logoRecently, I needed a MySQL conditionals that I had not used previously.   I needed to add a condition that included an OR condition on a single field.  While OR is available in the WHERE conditional, it along with the LIKE filter overly complicated my query.   Some OR conditionals become very complicated with the OR conditional.  I found that the MySQL function REGEXP was just what the doctor ordered.   It opened up a wide variety of options, and simplified the query.

Using REGEXP  turned out to be not only easy, but, convenient. We can use regular expressions.  If you are used to using regexp, you will love this feature. For example, assume you have fields called`price` and `view` and you want to find records with the price under $300,000 and a `view` of ‘ocean,’ ‘river,’ or ‘lake,’ you can use regular expressions. You may use the following SQL conditional:

WHERE `price` < 400000 AND `view` REGEXP ‘Ocean|River|Lake’

As with other places you can use regexp, this allows you to say you would like the field `view` to contain the Ocean, River or Lake … regardless of whatever else is in that field.   Therefore, it references the field once and incorporates the concept of LIKE.

Of course, you could request the field to contain:

  • ‘^Ocean$|^River$|^Lake$’ if you wanted one of these words to be the only word in the field.
  • ^Ocean|^River|^Lake’ if you want the first word of the field to be Ocean, River or Lake.
  • [a-z ]+ if you simply want to know that there is some view defined.

OMG, you can create any of the wonderful expressions that can be built with the patterns from the following table …

Try your hand at it, knowing you have the following features to build your regular expression:

Pattern What the pattern matches
^ Beginning of string
$ End of string
. Any single character
[…] Any character listed between the square brackets
[^…] Any character not listed between the square brackets
[a-z] Within[], dash (-) create a range of contiguous characters or numbers. Here we are specifying all characters from a to z and A to Z. MySQL ignores character case when using expressions
p1|p2|p3 Alternation; matches any of the patterns p1, p2, or p3
* Zero or more instances of preceding element
+ One or more instances of preceding element
{n} n instances of preceding element
{m,n} m through n instances of preceding element

In addition to the patterns used in building a filter, you should know that MySQL recognizes the escapes in strings. Therefore you can use \n,\r,\t,\$,\^,\\, etc to embed special characters in the search and to search for ^,$ and \.*

To finish off this memo, you need to know:

  • RLIKE (not to be confused with LIKE) is a synonym for REGEXP in mySQL
  • MySQL examines expressions independent of the character case.
  • Oracle’s REGEXP does not support escape characters found above.
  • You could install LIB_MYSQLUDF_PREGif you wanted an even more powerful expression handling in your mySQL
  • You can learn more about LIB_MYSQLUDF_PREG at http://www.regular-expressions.info/mysql.html

Using the MySQL function DATE_SUB

SQL logoRecently, I needed to craft a SQL query that would allow me to determine the records that had been stored in the last 30 days. I found DATE_SUB() was just the thing I needed for the query. Assuming you have a record field containing a date, such as a field `submitted,` if you would like to find all records submitted in the last 30 days, you could use the following SQL feature to the WHERE conditional:

AND `submitted` >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)

DATE_SUB() accepts 2 parameters; the date of interest, and a phrase of the form ‘INTERVAL ii xxxxx.’ DATE_SUB() subtracts the specified interval from the from the date of interest. So, if we want to find what has happened in the last 30 days, subtract the INTERVAL 30 DAY from the current date and then ask for everything after that Date in the query.

For completeness, let’s note:

  • CURDATE() returns the current date in the format yyyy-mm-dd.
  • INTERVAL accepts:
    • A number appropriate for the type of interval you choose,
    • The following interval types
      • MICROSECOND
      • SECOND
      • MINUTE
      • HOUR
      • DAY
      • MONTH
      • QUARTER
      • YEAR
      • and other*

*For more available interval types, see http://www.w3schools.com/sql/func_date_sub.asp

RETSfeed Handling

Rets LogoI have been doing quite a bit of processing of RETSfeeds over the last few years. To use a RETSfeed, you really do need a library to help simplify the processing, and you need to know what is available in the RETSfeed. Here are my suggestions …

Visit www.retsmd.com when you have the address of your RETSfeed and the login information. retsmd.com provides a very simple method for examining the RETSfeed to see what classes and variables are available to you on the RETSfeed.

If you are looking for a useful library to use for processing, Troy Davission has a good, free library available … PHRETS. This library is quick to access, easy to use and very useful. I have used it on several projects and it seems to blend with my PHP processing very nicely.

Continue reading

Warning: money_format does not work on Windows

Folks,

PHP logoI am certain you are far cleverer than I. I spent a couple of hours attempting to do some development with PHP function money_format(), and it did not work.

I wanted to use money_format() to convert a number formatted for currency. But, I wanted it function for development and for site.

I scratched my head over and over … leaving dandruff everywhere. After some reflection, and then search of the internet and reading the PHP documentation for money_format over and over … it dawned on me that the documentation said it does not work on a Windows system. I develop on a Windows system running Apache, and load to a server running Linux. Don’t use money_format if you develop on Windows … if you want to see function on your development environment.

Use number_format(), this works on both Windows and Linux. Much better.

Oh, and the currency symbol can be identified using localconv(), which returns an array with [currency_symbol].

Pinging and WordPress

WordPress LogoIf you have a WordPress site and would like the world to know when you release a new post, you should have an option for the visitors to subscribe to your site. But, before people will know about your site and subscribe, you have to let the internet know that you have articles for the world to read. Yes, the search engines find your new content eventually, but, you can be proactive. There are many ways to let the world know that you have articles on your site. You can:

  • Post to social websites (such as Facebook, twitter, etc)
  • Join forums to mention your blogs casually (after finding a relevant forum for your site)
  • You can use the 80/20 rule that you spend 80% of your time on other blogs sites getting people to know you as an authority by commenting on blogs, and providing information and content that refers back to your blogs
  • You can ping the world and announce the release of your new articles.

To gain authority in the blogosphere, you should do all these things. This article focuses on the last item … announcing the new post to the world. Ping the world. To ping the world via WordPress, go to Settings/Writing, and head to the bottom of that page. At the end of the page there is an area labelled “Update Services.” In the box within that section, add the list of sites you would like notified of your new post.

Here is a nice list of target addresses to add to your ping list:

http://api.feedster.com/ping
http://api.moreover.com/ping
http://api.moreover.com/RPC2
http://api.my.yahoo.co.jp/RPC2
Continue reading