Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.

SQL

mysql date/time functions

Posted by chris on May 12th, 2008 Comments(0)

Better than using PHP to time format date times stored in your database for easier human readability is using MySQLs built in functions:

To turn a date time stamp into a readable time on the 12 hour clock with an AM/PM signifier
TIME_FORMAT(date_time,’%h:%I:%l %p’) as time

Returning just the date time portion of date time stamp
DATE(date_time) as date

Make a date pretty
Unlike the actual dates I go on, in MySQL you can actually make her pretty, the following code would convert 2008-01-01 into Jan 01, 2008.
DATE_FORMAT(tour.date_time,’%b %d, %Y’) as date

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

In SQL ()

mysql running sql files

Posted by chris on April 21st, 2008 Comments(0)

From the MySQL command line use the database your SQL file will be affecting.

use database;

To execute the file.

source /path-to-file/file.sql

I always forget this one.

In SQL ()

MySQL Union

Posted by chris on January 22nd, 2008 Comments(0)
(SELECT invoice
FROM orders
WHERE date=NOW())
UNION
(SELECT invoice
FROM other_orders
WHERE date=NOW())
ORDER BY date
In SQL ()

MySQL Case Statement

Posted by chris on November 8th, 2007 Comments(0)

The database I was working on the other day is FAR from normalized. It stores shipping types as regular VARCHAR strings. We needed to orderby Overnight and International orders so they print first in the batch. Using a case statement I was able to create a temporary sane column in this query, using the integer values in this temporary shipType column I was then easily able to orderby and it only added 1/10 of a second to the query’s execution time. Here is an example of the MySQL case statement:

				CASE SUBSTRING_INDEX( shipping, '|', 1 )
				     WHEN 'Overnight' THEN 1
				     WHEN 'Overnight Delivery' THEN 2
				     WHEN 'International' THEN 3
				     WHEN 'USPS International Shipping' THEN 4
				     WHEN '' THEN 5
				     WHEN ' * Product(s) qualify for Free shipping * ' THEN 6
				     ELSE SUBSTRING_INDEX( shipping, '|', 1 )
				    END AS shipType 
In SQL ()

Querying MS SQL database looking for a specific column

Posted by chris on November 2nd, 2007 Comments(0)

At work we interact with a MS SQL server that our shipping software runs on. We are a MySQL shop so don’t use MS SQL too much and the database in this proprietary shipping system is pretty large (about 100 tables). I needed to find a table that another table had a relation too and only knew the column name. So I ran this

SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name = ‘Alias_ID’ )

in MS SQL 2000 Query Analyzer and returned a list of 6 tables.

A lot better to troll through 6 tables than 100 tables.

In SQL ()

Mod-Log-SQL – Storing Apache Access Logs in a MySql Database

Posted by chris on October 27th, 2007 Comments(0)

Mod log sql is an awesome way of getting away from those old log files and is really handy for both web development and system administration. It’s been a while since I’ve posted a blog and this is something I’ve never done before so here we go. If you are ever doing any kind of parsing of your apache access log and run a relatively high traffic website (the one I’m doing this for, mp3crib.com, averages over 5,000 hits a day and some days gets over 10,000) you will begin eating up huge amounts of CPU and memory (if storing the information in an array). Well lately it’s gotten so bad that my PHP script fails due to memory exhaustion. I can’t have that. I heard somewhere that databases are better than flat files, go figure. If I knew a lower level language then I would of course write my parser in that…but I don’t. So luckily libapache2-mod-log-sql exists.
Read the rest of this entry »

In Linux, SQL (, , , )

easy mysql backup

Posted by chris on July 6th, 2007 Comments(0)

Here’s an example of quick script I wrote in PHP and added as a cronjob to backup some mysql databases:

// dump database tables to sql text files //
exec('mysqldump -uroot -pPasswordHere music > /tmp/music.sql');
exec('mysqldump -uroot -pPasswordHere taskfreak > /tmp/taskfreak.sql');
// pack into tarballs //
exec("tar -cf /tmp/music.sql.tar /tmp/music.sql");
exec("tar -cf /tmp/taskfreak.sql.tar /tmp/taskfreak.sql");
// gzip tarballs //
exec("gzip /tmp/music.sql.tar");
exec("gzip /tmp/taskfreak.sql.tar");

In Linux, Programming, SQL ()

setting up iis7 w/ mysql and php

Posted by chris on May 2nd, 2007 Comments(0)

I recently bought a nice little HP laptop and got a really good deal on it too. It has a dual core pentium, 1gb memory, 120 gb hard drive, and everything else I need. However, it did come with Vista, which meant I needed to get php5 working with iis7 and mysql if i wanted to do an development work on it. Luckily I found this great how to http://blondr.blogspot.com/2006/11/set-up-iis-7-w-mysql-and-php-5.html

In Programming, SQL ()