Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Using Twitter, Part 3 - Offline Twitter

In my last post about the use of twitter, I said that my primary purpose for using twitter is to track my time. However twitter is not created for that use. It is very difficult to track your time using twitter. But there is an easy solution to this problem - create an offline twitter.

Twitter is a huge application - but offline twitter is not. The whole application is just 4 lines of PHP code. Yes - that's right - just 4 lines. And a database.

Database

The database is just one table(OT) with 3 fields. The simplest solution is often the best.


CREATE TABLE `OT` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `status` varchar(255) NOT NULL,
  `time` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM ;

PHP Code

This code will be the entire application(if you can call it that). Just save this to a file called, say, OT.php

<?php
if(!$_REQUEST['status']) exit;
mysql_connect('localhost','root','pass') or die("Cannot connect to mysql Server");
mysql_select_db('Data') or die("No database called 'Data'");
mysql_query("INSERT INTO OT VALUES('','$_REQUEST[status]',NOW())");

If you want to use a database abstraction layer, be my guest. If you escape the $_REQUEST[status] before using it in the query, by all means do so. But, the result will be the same. An offline twitter using just 4 lines of code.

Input

So, how does one enter new status into this system? By making a small modification to the command line twitter client...

curl --data-ascii "status=`echo $@|tr ' ' '+'`" "http://localhost/tools/OT.php"
curl --basic --user "<User>:<Password>" --data-ascii "status=`echo $@|tr ' ' '+'`" "http://twitter.com/statuses/update.json"

The first line will send the status to my PHP file and the second will send it to the twitter server. Now I have all the status in the database.

View the Contents

I have still not created an interface that will use this database to show my time useage. If I want to know something, I just open up phpMyAdmin and run an SQL query.

I know - this method is not for everyone. You need to be on linux to use my command line client. You must have a web server and a MySQL server running for this method to work. But if you are working on the LAMP platform, you will have this stuff ready.

Read More...

Bin-Co and OpenJS Backend Updated

I have just finished updating the backend of my two sites Bin-Co and OpenJS. I have updated the Database Design to make it compactable with my DB Design rules. Also some part of the code base have been altered to make it easier to reuse.

Using a custom CMS has its advantages - but it has a big maintance load. The good part is that a lot of cool PHP code was created - which I will be releasing in my PHP page.

There is a problem with this upgrade - there is a possibility of errors somewhere in the site. If you notice any problems, let me know.

Once that job is complete, I will redesign this blog.

Read More...

Invalid JSON

I just read a post in Simon Wallace's blog about Invalid JSON. The main point is that valid JSON strings must be enclosed in a double quote(") - not a single quote(').

In JSON (unlike in JavaScript) these MUST be double-quoted strings. In fact, ALL strings in JSON must be enclosed in double quotes (JavaScript also allows single quotes; JSON does not).

Sorry guys, but my JSON generation function created JSON with single quotes. I have already updated and uploaded the modified version - but if you are using the old version, get the modified function from the sql2json() page.

If you have never heard of this function before, sql2json() function will take a SQL query as it argument, execute it, fetch its result, convert it to a JSON string and then return the JSON string. This is extreamly useful if you are creating Ajax applications and decide to use JSON over XML. If you have decided to use XML, take a look at my XML Parser for JavaScript instead.

Guess I should have read JSON's RFC(RFC 4627 - text file) before creating the function. And I have been creating a lot of converters recently - the great '2' functions.

sql2json()
Converts SQL Results to JSON String - the function we were talking about just now.
xml2array() for PHP
Converts a XML string to a PHP array. xml2array() is a easy to use PHP function that will convert the given XML text to an array in the XML structure. Kind of like my Javascript xml2array() function.
xml2array() for JavaScript
Converts a XML document to a JavaScript array.
Read More...

SQLite

SQLite Logo

SQLite is one of the smallest DBMS available. Unlike other RDBMS solutions like MySQL, PostgreSQL etc. SQLite does not have a Client-Server architecture. SQLite is a embedded into the software using it. The main advantage of this is you can create an application that uses a database without expecting the end user to have a database server.

SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine.
SQLite Official Site

SQLite is in no way a competition for MySQL or PostgreSQL as they tend to different needs. In the context of the LAMP platform, it is rare to see SQLite being used in a web server as most servers have MySQL(or PostgreSQL) server running. SQLite is more relevent in the application field where the user may not have a server running.

In client-server model RDBMS like MySQL, PostgreSQL, etc. there will be a server running in the background at all times. For example, take MySQL. It uses a background process 'mysqld'(mysql daemon). When a program(like PHP on Apache) tries to execute an SQL command, this program will communicate with the background process and give the SQL command to the server. The server will then execute it and return the status.

In contrast, SQLite is embedded into the application that uses it. For example amaroK music player, uses SQLite to store its playlist and media library data. This application can read and write to the database using SQL commands without the need for a database server to be running on the user's system.

Some well known applications that use SQLite are...

  • amaroK MP3 Player
  • Yum - Yellow Dog Updater
  • Banshee Music Player

(Well, they are well known if you use linux.)

Features

The features for SQLite include...

  • ACID compliant.
  • Zero-configuration - no setup or administration needed.
  • A complete database is stored in a single file.
  • Backing up, Exporting/Importing is very easy
  • Supports databases up to 2 terabytes in size.
  • Sizes of strings and BLOBs limited only by available memory.
  • Very small code footprint.
  • Faster than popular client/server database engines for most common operations.
  • Easy to use API.
  • Self-contained: no external dependencies.
  • Open Source

Filed Under...

Read More...

PostgreSQL

PostgreSQL is a RDBMS that many claim is better than MySQL. This is basically the only Open Source competion MySQL has. There are other Open RDBMS but they are not in the league of MySQL and PostgreSQL.

PostgreSQL is a open source object-relational database server(database management system). It supports many advanced features that are not in MySQL.

Features of PostgreSQL

Functions
This feature allow blocks of code to be executed by the server. Functions in PostgreSQL can be written many languages like PL/pgSQL, PL/Perl, plPHP, PL/Python, PL/Ruby, PL/Tcl and more.
Indexes
An index is storing some columns of a table separately to allows quick access to the table. This makes the index smaller than the original table (due to having fewer columns), and it is optimized for quick searching. Most RDBMS supports this.
Triggers
A database trigger is procedural code that is automatically executed in response to certain events on a particular table in a database. Triggers can restrict access to specific data, perform logging, or audit access to data. In addition to calling functions written in the native PL/PgSQL, triggers can also invoke functions written in other languages like PL/Perl.
MVCC
PostgreSQL uses Multi-Version Concurrency Control (MVCC), which gives an user a copy of the database, allowing changes to be made by one user without being visible to the other users until the change is committed. This also lets you 'roll back' to an older version of the database if necessary. This is similar to the Version control systems like CVS or Subversion.
Inheritance
This feature lets tables inherit the properties of a parent table. Data is shared between parent and child tables. For example, adding a column in the parent will cause that column to appear in the child table also.
Referential Integrity
PostgreSQL allows column constraints, foreign key constraints, and row checks.
Other features
And many more features like...
  • Views
  • Full, inner, left and right joins
  • Sub-selects
  • Encrypted connections via SSL
  • Point-in-time recovery

Another open source RDBMS is SQLite. This provides no serious competion for MySQL or PostgreSQL as it tends to a different need. It is a a small C library that implements a self-contained, embeddable, zero-configuration, single file SQL database engine. More about SQLite in the next post.

References

Comparisons

Read More...

MySQL Storage Engines - MyISAM, InnoDB and others

MySQL has a feature called Storage Engines - it supports multiple independent storage engines that can be used as handlers for different table types. Each table can be stored in a way that is most optimized for that particular table.

With this system, the developer can choose how a table must be stored - he can chose which storage engine is best for that particular situation. For example in a content management system, the table containing the contents will be read more often - so we will chose a storage engine better optimized for reading data(eg. MyISAM). But in the case of the table storing the logs, it must be written more often - so we chose a storage engine that is better at writing for that table. Having multiple storage engine makes sure that you can get the best optimization for any given situation.

There are many who believe this is a Bad Thing. In most other DBMS, the Administrator don't have to worry about such details - the DBMS will take care of this. I don't want to take sides in this debate - as for many people, this has become a religious issue. Anyway, as I am writing a series on MySQL, I cannot ignore this very important feature of MySQL.

I have only used two of MySQL many Storage Engines - MyISAM and InnoDB. The rest, I have not uses yet. Someday.

MyISAM

MyISAM is the default storage engine for MySQL. If you create a table in MySQL, without paying any attention to select a specific engine, then that table will be created in MyISAM(by default). This storage engine is the best for high-speed storage and retrieval. It also supports fulltext searching capabilities.

MyISAM is supported in all MySQL configurations. This engine is used for Data warehousing, read-only archives optimized for bulk operations, etc. as it is the best solution when you want flexible index formats, big scans and has a small disk footprint for data and index. This is not suitable for a typical OLTP application because of table-level locking, bad crash durability and lack of ACID compliance.

InnoDB

The InnoDB storage engine provide transaction-safe tables(BDB also does this). InnoDB is also included by default in all MySQL 5.0 binary distributions. This engine supports...

  • Row level locking
  • MVCC
  • Savepoints within transactions
  • More features for typical OLTP applications.

One of the main advantage of this engine is that you can rollback to a previous version of the database using its COMMIT/ROLLBACK support. InnoDB engine is more reliable than MyISAM.

Memory

The MEMORY storage engine provides in-memory tables. The MERGE storage engine will let you handle many identical MyISAM tables as a single table. Like MyISAM, the MEMORY and MERGE storage engines handle non-transactional tables, and both are also included in MySQL by default.

The main advantages of the MEMORY Storage engine are...

  • No disk footprint
  • Extremely fast

This is most useful for...

  • Summary tables
  • Simulations
  • Temporary operations(sessions etc)

CSV

The CSV storage engine stores data in text files using comma-separated values format. The main advantages of this approach are...

  • Very simple - data stored in plain text files
  • The database can be modified without the server
  • Export/Import couldn't be easier.

Other Storage Engines

There are many other storage engines supported by MySQL. Some of them are...

  • NDB Cluster
  • ARCHIVE
  • BLACKHOLE
  • FEDERATED
  • Falcon

If you are not satisfied with any of these engines, you can write your own engine.

Reference

Read More...

phpMyAdmin Tips and Tricks - Database Backup and Restore

phpMyAdmin is a big software - there are features that I have not used yet. But there are some features that are not very obvious - but could save you a lot of time.

Quick Table Browse

The normal way to seeing the contents of a table is to click on the table name in the left pane and then click on the 'Browse' tab in the top. But there is a one click way to do this - click on the small icon next to the table name in the left pane.

Click icon to browse table

Default Table Selection

When you access phpMyAdmin, you get the main page - after that you have to chose from the drop down which database should be used. Wouldn't it be useful if the database you use most would be automatically selected on loading? You can do this by accessing phpMyAdmin using the following URL.

http://localhost/phpMyAdmin/?db=my_fav_db

You will have to change the database name and the location to the one in your system - but you got the idea right?

Database Backup

There are many ways to take a database backup using phpMyAdmin - but my favorite way is to take an SQL Dump.

Select the database you want to backup and click 'Export' tab in the main frame. Now select all the tables in the select box. You can select the format of the backup - like SQL, CSV, XML etc. We will select the SQL option. It is recommended that you check the 'Add DROP TABLE' and 'Add IF NOT EXISTS' checkboxs. This will make sure that the existing tables will be removed when restoring the backup. Now check the 'Save as file' checkbox. Then click the 'Go' button. Now you should see a download option for the database backup. You can save this file to you harddisk and run it later if you want to restore the backup. If you did not check the 'Save as file' option, the SQL dump will be shown in a textarea.

This can also be done using the 'mysqldump' command in linux using the following command.

mysqldump --add-drop-table --user=root DATABASE> backup_file.sql

Table Backup

If you just need the backup of a single table, select that table from the table list in the left pane and click the 'Export' tab. Now do the steps described in the database backup section. Here only the selected table will be backuped.

This can be done using the following command.

mysqldump --add-drop-table --user=root DATABASE TABLE> backup_file.sql

Restoring a backup

Click the 'SQL' icon(SQL Icon) in the left pane - this will open a popup. Paste the SQL code you when you backuped the database in the textarea in this popup. Alternatively, you can chose the 'Import Files' tab in this popup and chose the location of the file you saved when backing up the data.

This is not a recommended method to restore large backup - if the backup file is more than 1 MB, don't try this. A better method is to upload the backup file to you host and restore the backup using the shell.

Read More...

Installing phpMyAdmin 2.8

phpMyAdmin Logo

The install instructions I provided earlier will only work in the earlier versions of phpMyAdmin. The latest version(2.8.2.1 - as of Aug 15, 2006) has a much easier way of doing this.

Get the latest version of phpMyAdmin from phpMyAdmin.net. The files will compressed - so you will have to extract it. Create a folder called 'phpMyAdmin' in the document root of your server and extract the PHP scripts into it. That's it - the installing part is over. The latest version will automatically configure itself to run with these settings...

  • Database Host : localhost
  • Username : root
  • Password : (Empty)
For the example we provided the last time, this is all that is needed. But if you wish to configure your phpMyAdmin using more advanced options, open the folder you installed phpMyAdmin to(<Document Root/phpMyAdmin/>) and create a folder called 'config'. Make sure it has write permission. You can give it write permission with the following command(assuming that your are on a linux system)...
chmod 666 config

Now open the URL http://localhost/phpMyAdmin/scripts/setup.php. This will help you configure your phpMyAdmin.

The configuration is very straight forward - so I am not providing much details about it. If you have any doubt, reffer the documentation that comes with the installation.

After you have finished configuration, click on the 'Save' Button in the 'Configuration' section. This will create a 'config.inc.php' file inside the 'config' folder that we created earlier. You will have to manually copy this config file to you phpMyAdmin folder. After this is done, delete the config folder. You can do this with the command...

mv config/config.inc.php .
rmdir config

On a personal note, I still prefer the 2.6 Version of phpMyAdmin. Some pages in the latest version have a tendency to create horizontal scroll bars even on 1024x768 resolution.

Read More...

Installing and Configuring phpMyAdmin

phpMyAdmin Logo

In the last post, I introduced phpMyAdmin. In this, we will deal with installation and configuration of phpMyAdmin.

UPDATE: This installation manual is for the older versions on phpMyAdmin. To see how to install the latest version, see the post Installing phpMyAdmin 2.8

My aim is to create a tool that could be used to administer your database in a development server. These directions are NOT for a production server. You will need much more security when installing phpMyAdmin on a production server. A good source for information on how to install phpMyAdmin securely is the series 'Doing more with phpMyAdmin'.

Installation phpMyAdmin

Get the latest version of phpMyAdmin from phpMyAdmin.net. The files will compressed - so you will have to extract it.

Create a folder called 'phpMyAdmin' in the document root of your server and extract the PHP scripts into it. That's it - the installing part is over.

Configuring phpMyAdmin

Necessary Information

Now we have to configure it - before starting this, make sure you have the following details...

  • Database Server address
  • Database Username
  • Database Password

For the sake of example I am going to assume that the following values...

  • Server : 'localhost'
  • Username : 'root'
  • Password : ''

Using the 'root' user with an empty password is one of the worst security blunders you can make - but what the hell, we are on a development server, right?

Editing 'config.inc.php'

Open up the file 'config.inc.php' in your favorite editor and edit the following values...

$cfg['PmaAbsoluteUri'] = 'http://localhost/phpMyAdmin/';

This is the absolute path for your phpMyAdmin directory. If you enter this location into a browser, phpMyAdmin should show up. I will not recommend that you use 'localhost' as the domain if you are on a network and expect others to connect to this phpMyAdmin setup. You should use 'http://<YOUR IP</phpMyAdmin/' in such a case. However if you are using phpMyAdmin for just your system, localhost will do fine.

$cfg['Servers'][$i]['host'] = 'localhost';

This is the database server address. If you look at the following lines in the file, you will notice that there is more than one instance of this line. This is for managing more that one database server using just one phpMyAdmin. Make sure that all the other hosts are set to empty - phpMyAdmin will ignore it if it is empty.

$cfg['Servers'][$i]['user'] = 'root';

The database user - in our case 'root'. Only the first instance of the line should be set - the other will be ignored if the 'host' option is not set.

$cfg['Servers'][$i]['password'] = '';

The password for the above given user - in this example we are using an empty password.

There are many other options - but you will not have to worry about them - they will do fine with their default values. However, if you are adventurous, feel free to experiment with the configuration options. The file well commented in detail - so you will have no trouble finding your way around - if you know a bit of PHP.

After all this is done, open up a browser and point it to http://localhost/phpMyAdmin/. If all went well, you will get the front page of phpMyAdmin.

References

Read More...

Managing Databases with MySQL Clients - phpMyAdmin

There are many MySQL Clients(or database administration tools) out there - MySQL Front End, Tora, mysql etc. But in the web environment, one client rules - phpMyAdmin. phpMyAdmin is a PHP based(obviously) GUI administration tool for MySQL.

When I began programming in Linux, I could not use the DB Administration tool my co-workers were using. They were using MySQL Front End - and only the windows version was available for it. Since no one in my office used Linux, none knew another DBA for Linux. One suggested Tora, but I could not get it to run - some dependencies were missing. Anyway, I was forced to use mysql - yes, the command line client that came with MySQL Server. You can start it by bringing up a terminal and entering 'mysql' into it. Then you type every SQL command you want to execute into it. Trust me, it was not fun. The biggest problems was that I want to copy something and press 'Ctrl+C' - in the terminal 'Ctrl+C' is not Copy - its Close. So the client goes down. It was very irritating. Anyway I found phpMyAdmin before long and have been using it ever since(even after I got Tora running).

The biggest advantages of phpMyAdmin is that it is web based - it runs on any server capable of handling PHP. Because of this all the online Database Administration is done using phpMyAdmin. If you have a site of your own, and you have the control panel access to it, you will know what I mean. They database management part will be handled by phpMyAdmin. The phpMyAdmin site cites the following as the features of phpMyAdmin...

  • Browse, view and drop databases, tables, views, fields and indexes.
  • Create, copy, drop, rename and alter databases, tables, fields and indexes.
  • Maintenance server, databases and tables, with proposals on server configuration.
  • Execute, edit and bookmark any SQL-statement, even batch-queries.
  • Create and read dumps of tables - in various formats like CSV, SQL, etc.
  • Export data to various formats: CSV, SQL, XML, Excel and more.
  • Administer multiple servers.
  • Manage MySQL users and privileges.
  • Check referential integrity in MyISAM tables.
  • Using Query-by-example (QBE), create complex queries automatically connecting required tables.
  • Search globally in a database or a subset of it.
  • Support InnoDB tables and foreign keys.
  • Support mysqli, the improved MySQL extension.
  • And more...

phpMyAdmin is so commonly used that there is a book - Mastering phpMyAdmin for effective MySQL Management - that aims to teach it.

Reference

Read More...

MySQL - Database Management System

In this LAMP series, we have looked at Linux and Apache - now we are going to have a look at MySQL.

MySQL is a multithreaded, multi-user, SQL based Database Management System(DBMS) under GPL license. It have become the most popular open source database because of its performance, reliability and ease of use. It is said to have more than 10 million installations.

The popularity of MySQL as a web application platform is closely tied to the popularity of PHP, which is often used with MySQL and nicknamed the Dynamic Duo.

MySQL have long been my favorite DBMS because its ease of use and great support in PHP. I have used MySQL in almost all the Web applications I have created over the years. This includes PHP as well as Perl apps. Ruby on Rails, the hottest framework right now, uses MySQL.

Features

Please note that some of the following features are only present MySQL 5.

  • Cross-platform support
  • Stored procedures
  • Triggers
  • Cursors
  • True VARCHAR support
  • INFORMATION_SCHEMA
  • Multiple independent storage engines(more about this later)
  • Transactions with the InnoDB, BDB and Cluster storage engines; savepoints with InnoDB
  • SSL support
  • Query caching
  • Sub-SELECTs (or nested SELECTs)
  • Full-text indexing and searching using MyISAM engine
  • Embedded database library
  • Full Unicode support
  • ACID compliance using the InnoDB, BDB and Cluster engines

Used In

Some of the most popular sites using MySQL are given below...

Read More...

Subscribe to : Posts