Archive

Archive for April, 2009

PHP’s error reporting levels

April 30th, 2009 Comments off

The drudgery and pain of debugging and fine-tuning PHP can be alleviated when one takes full advantage of PHP’s various  error reporting levels that can be changed both in the config and on the fly at run time. To change the error reporting level on the fly during execution of a script, use PHP’s error_reporting() function (http://us.php.net/manual/en/function.error-reporting.php).  The error_reporting function takes a single int parameter which can be anyone of the following values

  • 1 (E_ERROR)
  • 2 (E_WARNING)
  • 4 (E_PARSE)
  • 8 (E_NOTICE)
  • 16 (E_CORE_ERROR)
  • 32 (E_CORE_WARNING)
  • 64 (E_COMPILE_ERROR)
  • 128 (E_COMPILE_WARNING)
  • 256 (E_USER_ERROR)
  • 512 (E_USER_WARNING)
  • 1024 (E_USER_NOTICE)
  • 2048 (E_STRICT)
  • 4096 (E_RECOVERABLE_ERROR)
  • 8192 (E_DEPRECATED)
  • 16384 (E_USER_DEPRECATED)
  • 30719 (E_ALL)

Let’s say you actually wanted to implement the E_STRICT level in a single PHP script. Adding the following code, most likely at the top of your script will handle this:

<?php
error_reporting(2048);
?>

Downloading and Converting EWS BulkDownload TSV Files

April 28th, 2009 Comments off

For the longest time, I avoided using the BulkDownload service of Yahoo’s Enterprise Web Services (EWS) for managing our daily pay per click (PPC) account. Now this wasn’t because I did not want to use it, I just thought it was was going to be a severe pain to get it implemented. The reasoning for my hesitation in implementing the BulkDownload feature was because I was pretty ignorant to different encodings. Now that my knowledge of encoding types has blossomed over time, I felt I was ready to tackle the project of using and implementing Yahoo’s BulkDownload service in a complete daily account synchronization for avery large PPC account structure.

When using Yahoo’s EWS BulkDownload service, you can request one of two file formats to be returned to you. These two file formats are:

  • EXCEL_XML – an Excel 2003 XML format
  • TSV (tab seperated value)

Here is the trickey part: the EXCEL_XML files are UTF-8 encoded and the TSV files are returned in UTF-16LE encoding.  Normally, I like to use comma seperated value (CSV) file formats and use the LOAD DATA [LOCAL] INFILE  syntax of MySQL. (One can just as easily use TSV file formats with the LOAD DATA [LOCAL] INFILE syntax. You just need to make sure you specify

FIELDS TERMINATED BY '\t'

instead of

FIELDS TERMINATED BY ','

in your SQL statement.) Bacause of the TSV file coming in as UTF-16LE encoded, simply grabbing the tab seperated value file and loading it right into MySQL would not work. My solution would just involved a couple extra steps; once I retrieved the account structure file from Yahoo, I would simply loop back through the file and convert the data of the file from ‘utf-16′ to ‘ascii’ using PHPs iconv() function. This can obviously be done in multiple ways, but here would be one way to accomplish the task:

while (!feof($handleIn))
 {
    $content = iconv('utf-16', 'ascii', fread($handleIn, 8192));
    fwrite($handleOut,$content);
 }

Once the file has been successfully converted over to ascii, I can then run a LOAD DATA [LOCAL] INFILE to get the TSV file into a MySQL table:

LOAD DATA local INFILE '[file_name]'
INTO TABLE [table_name]
FIELDS TERMINATED BY '\t' optionally ENCLOSED BY '\"' LINES TERMINATED BY '\n'
IGNORE 2 LINES;

The reason for the IGNORE 2 LINES is because the first two lines of TSV file are basically header rows and I consider them to be junk rows.

Selecting a result set into a Comma Seperated Value (.csv) file

April 14th, 2009 Comments off

Getting a result set from MySQL into a comma seperated value (.csv) file does not have to be a difficult adventure.  In the past, I might have written my query in SQLyog, select the query, hit F9 to run the query, and click on the button to Copy All Rows to Clipboard, paste the copied results into an Excel spreadsheet and save the Excel spreadsheet at a .csv file. Trust me though, there is a better way!

To save a result set into a .csv file from your query, check out MySQL’s SELECT…INTO OUTFILE syntax. In the following example, I want to create a comma seperated value file that contains a list of all cities in the state of Michigan from my cities table. The syntax to do so, on my Windows machine, would look like:

SELECT     city, state_abbrev, country_code
INTO OUTFILE 'C:\temp\cities.csv'
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
FROM     cities
WHERE     state_name = 'Michigan'
ORDER BY city ASC

To get more information on this nifty feature of MySQL, check out the SELECT…INTO OUTFILE manual page (http://dev.mysql.com/doc/refman/5.0/en/select.html) at mysql.org

Easily grabbing the hour from PHP

April 14th, 2009 Comments off

Ever need to grab a portion of the current timestamp from PHP? Know how to grab the hour of the current time the server in MySQL using the HOUR() function, but not quite sure how to grab the hour through PHP? Try this handy little function in PHP:

idate(format)

According to the PHP manual page on idate (http://us3.php.net/manual/en/function.idate.php), the idate() function will:

“Returns a number formatted according to the given format string”

To make things easy, the idate function accepts only one format parameter and returns a single integer. To quickly and easily retrieve the hour of the current timestamp, just pass in the H parameter to the function.

For example:

$iHour = idate(‘H’);

To get the full list of parameters you can pass to the idate function, to see how the idate() differs from date(), or to find out more specifics, visit the idate function reference page here:  http://us3.php.net/manual/en/function.idate.php

Categories: PHP Tags: , ,

Fake Latin Content Generator

April 11th, 2009 Comments off

Stumbled across this nifty little tool for generating fake Latin content:

http://websitetips.com/articles/copy/loremgenerator/

This type of tool can be extremely useful when you are in the design stage of developing a new website. By using a tool like this, you can generate X number of words to act as content so that you can fine tune your layout.

Converging SEO and PPC

April 10th, 2009 Comments off

Found this to be an excellent read on how your Pay Per Click initiative can help support your Search Engine Optimization efforts:

http://www.adcentercommunity.com/blogs/analytics/archive/2009/04/06/not-ppc-not-seo-but-keyword-marketing.aspx

Categories: Keyword Marketing Tags:

MySQL: Order By NULL

April 9th, 2009 Comments off

I amaze myself sometimes. For a long time now, I really never put two and two together about MySQL query performance and the fact that MySQL, by default, does a sort on the returned result set. Now, I have not seen any documentation on how MySQL chooses the order by column it chooses.  Neither did I realize that simply adding an ORDER BY NULL clause at the end of queries you do not need sorted, could increase query performance by quite a bit. I have not done any hardcore benchmarking against this, but there I experienced about 65% performance increase in a couple queries that returned 500,000 rows.

Categories: MySQL Tags: