[ Curiosity,Experimentation ]

Random stuff from the parallel universe of Ones and Zeroes

Posts Tagged ‘PHP’

An abstraction for MySQL transactions in PHP

Posted by appusajeev on January 16, 2010

PHP comes with intrinsic support for MySQL. A handful of functions exist for performing various database transactions. But when the number of pages containing database interactions increases,it becomes a pain to handle the connections and query processing on each page.This also affects the portability of the app since you have to make quite a lot modifications to up the app in the  new server environment. So i thought of creating a class ,which besides providing some abstraction ,can be reused.By reusing the class,ease of portability may be achieved since the environment dependent area has been isolated to a singe module/class(software engineering principles,he he).

The class exports  methods like connect(), selectdb(),query(),get_entry(),count_result() which are nothing but  a higher level abstractions over functions like mysql_connect(),mysql_query() while the other parameters like link identifier ,result resource etc remains hidden from the user.
The result of the query(SELECT query) becomes available in the array row with field name as the key.I think the rest is all self explanatory.
The script file containing the class can be included in the required page using include() PHP function.

Download Source

An abstraction for PHP MySQL  transactions

An abstraction for PHP MySQL transactions

Advertisements

Posted in PHP | Tagged: | Leave a Comment »

A URL Shortening Service with Code [PHP]

Posted by appusajeev on January 5, 2010

Alright,so we gonna create a URL shortening service in PHP this time,similar to tinyurl. The thing is pretty simple,involves basically database interactions only (nevertheless,tinyurl is a perfect example of how an idea can change the world,however simple it may be).

Anyway i have launched the service.Check it out:  http://su.coolinc.info (I couldnt find a shorter domain)

The URL entered by the the user is mapped to a short string which could be either the alias entered by the user or a random string of length three(the length can be increased in case we run out of new combinations,then again 26*26*26 combinations should suffice (using only lower case alphabets)).This mapping is stored in a table url_short with fields url and alias (MySQL is the DBMS).
When the shortened URL  is executed,the original URL corresponding to the alias is fetched from the database  and the user is redirected to that page.

Heres the source of init.php,to generate the database and table for use. You will have to modify the argumets of mysql_connect() (server,usename,password) depending on the settings of the hosting provider.The below code is what i ran on my computer.

Setup script to create the database

Setup script to create the database and table (MySQL)

Source of  index.php,the main page

Source of index.php, the main page

Source of index.php, the main page

Source of put.php,containing the script to insert the mapping into the database .

Source of put.php, the script to perform database interactions

Source of put.php, the script to insert into database

Posted in PHP | Tagged: | 8 Comments »