Archive for the ‘MySQL’ Category

Document Formats

Thursday, April 3rd, 2008

Documents

In FLOSS circles, March 26 is celebrated as the Document Freedom Day.

Document Freedom Day (DFD) is a global day for document liberation. It will be a day of grassroots effort to educate the public about the importance of Free Document Formats and Open Standards in general.

This is when I realized that people actually use the office packages daily. I almost never use it. According to me there are three options to store text data.

  • Plain Text
  • HTML
  • Database

Plain Text

The simplest format there is. If there is something I should remember, I just put it in a text file and save it to the desktop. I used to use it a lot earlier - but I don’t use it much nowadays due to searchability issues. There are quite a few advantages in using the text format

HTML

If I need any formatting in the text, I create the document in HTML. It is easier for me to create the formatting using HTML code that using WYSIWYG Word Processors(like MS Word). I write all my blog posts in HTML - perhaps the only occasion where I need formatting.

Database

My favorite method to store text data is in a database. I am a web developer - so I always have a Web Server and Database server running on my system - so this system is perfect for me.

Interface

Remove that scared look on your face - I don’t use phpMyAdmin or any Database Administration tools as the interface to save/view the data. I use my own custom scripts or WordPress.

The best example of this is txt. Txt is my code snippets/commands repository. You can view the full story in the Saving Code Snippets post.

That’s an online example - I also have a personal wordpress blog running in my local server. I use it to record events, purchases, store receipts etc.

Advantages

Tagging
Tagging is heaven-sent to make information more findable - any del.icio.us user should know that. I used to install Ultimate Tag Warrior to get this feature - but WordPress now supports tags natively.
Searching
Searching for data within a database is much more easier, faster and provides more relevent results than searching for the data in a collection of file. Even if you are using a file indexing software like Google Desktop Search or Beagle/Recoll(for linux users), I find database searching more easier. Another advantage of using database to search is that you can create complex queries if you know SQL.

Disadvantages

Not for everyone
Let’s face it - installing and maintaining a web/database server is a tad in the geek zone. An average Joe will find it just a little bit out of their league.
Overhead
Running a web server and a database server is a bit demanding on the RAM.
Backing up a little more complicated - but easier
Backing up the data in a Database is not as straight forward as backing up files - but its actually easier if you know how.

Online

One extra method to store the data - online. This data is stored in a database - but you don’t have the disadvantages associated with using a database. More and more people are turning to this method now. I did not include it in the initial list because its not a data format - its more of a data storing method.

Hello Script for PHP

Saturday, March 15th, 2008

In the last post I introduced the concept of ‘Hello Script’ - a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language. I thought I can elaborate on that concept by creating Hello Scripts for all the languages that I am familiar with.

Let’s start with PHP - I already provided this as an example for the last post. Here is the entire PHP Hello Script…


<?php
// Printing(IO)
print "Hello World!\n";

// Variables, concatenation
$name = 'Binny';
$year = 2008;
print "Hello, " . $name . " - welcome to " . $year . "\n";

//If,else conditions
if ($year > 2008) {
	print "Welcome to the future - yes, we have flying cars!";
}
else if($year < 2008) {
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!";
}
else {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.";
}
print "\n";

// For loop
for($i=0; $i<3; $i++) {
	print "$i) Hi there!\n";
}

//Numerical Array, While
$rules = array(
	'Do no harm',
	'Obey',
	'Continue Living'
);
$i = 0;
while($i<count($rules)) {
	print "Rule " . ($i+1) . " : " . $rules[$i] . “\n”;
	$i++;
}

// Associated array, foreach
$associated = array(
	‘hello’	=>	‘world’,
	‘foo’	=>	‘bar’,
	‘lorem’	=>	‘ipsum’
);
foreach($associated as $key => $value) {
	print “$key: $value\n”;
}

// Using Join and Split
$csv_values = explode(’,', “hello,world,how,are,you\n”);
print implode(”:”, $csv_values);

// Function, argument, return, call
function hello($name) {
	return “Hello ” . $name;
}
$hello_string = hello(”Binny”);

// One for the OOP fanboys - Class, members, object and stuff.
class Movie {
	public $name = ”;
	public $rating = 0;

	function __construct($name) {
		$this->name = $name;
		$this->rateMovie();
	}
	function rateMovie() {
		$this->rating = (strlen($this->name) % 10) + 1; //IMDBs rating algorithm. True story!
	}

	function printMovieDetails() {
		print “Movie : {$this->name}\n”;
		print “Rating : ” . str_repeat(’*', $this->rating) . “({$this->rating})\n\n”;
	}
}
//Create the object
$ncfom = new Movie(”New Country for Old Men”); //It’s a sequel!
$ncfom->printMovieDetails();

// File IO
// File reading, easy method…
$contents = file_get_contents(’Hello.php’);
print “Hello has ” . strlen($contents) . ” chars\n”;
// Writing to a file
$file_handle = fopen(’/tmp/hello.txt’, ‘w’);
fputs($file_handle, “Hello World”);
fclose($file_handle);

// Command Executing
print `ls`; //Execute the command ‘ls’ and print its output
print “\n”;

// Regular Expressions
$string = “Hello World”;
if(preg_match(’/^Hell/’, $string)) print “Yup - its evil\n”;
print preg_replace(’/l([^l])/’, “$1″, $string); //Remove an ‘l’ from both words. Should print ‘Helo Word’

/*
 * Specialized code
 */
// Database connectivity(native)
mysql_connect(’localhost’, ‘root’, ”) or die(”Cannot connect to the Database server”);
mysql_select_db(’Data’) or die(’Could not find a database called “Data”‘);

// Executing Query
$sql_handle = mysql_query(’SELECT name,url,description FROM Comment LIMIT 1′) or die(’Query Error: ‘ . mysql_error());
while($row = mysql_fetch_assoc($sql_handle)) {
	print “Name	:	$row[name]\n”;
	print “URL	:	$row[url]\n”;
	print “Desc	:	$row[description]\n\n”;
}
/* Just a note here - if you are going to use PHP with database in a production
 * system(and you will, trust me), use a Database abstraction layer rather than
 * the above mentioned native methods.
 */

print $_REQUEST['username']; // Method to get the value of the field ‘username’ after a form submit. Will not work at CLI execution

Save this to a file and keep it around for future reference - if you are just starting out with PHP

Coming up next - Python Hello Script.

Subscribe to Feed