My stream of consciousness being a programmer.

Tuesday, November 6, 2012

CRUD application with Yii framework.

Image result for yii

Please note that, this is a very minimal sample application done for a USER model using yii framework.

How to run,

1 – Please download the attachment.  http://www.mediafire.com/download/qd8zjcc94b4h3gc/yiiDemo.zip

2 – Create a db called “demoYii” and import the script file named (demoYii.sql) in mysql database. (using phpMyAdmin)

3 – Place the downloaded code inside htdocs folder. (if your using wamp, you can place it inside the www folder)

4 – Thats all, now you can view the application through following URL.  http://localhost/yiiDemo/web/index.php

NB: You need to login in order to view the crud therefore please use the following credentials.

User : admin, Password : admin

Monday, August 13, 2012

NIC me, another andorid app

Happy to announce my android application which i have named as Nic Me (nik me).. A basic app which will give you the birth day and some other information ones you enter your NIC number. It is my second attempt with android and stuffs so may not be that professional and standadrd. Grin

I have included the android application file (.apk) below, those who like are welcome to play around with my tiny app.

download here - http://www.azraar.com/NicMe.apk

qr code –

Image result for qr code

suggestions/feedbacks/ideas are mostly welcomed as i am a newbie to android.

Thanks for Reading.

Wednesday, May 9, 2012

No CHECK Constraint in MYSQL



It looks like MYSQL doesn’t really validate for check constraint. I checked for mysql manual and noticed its true.

Copied the following section from MySQL manual. (http://dev.mysql.com/doc/refman/5.5/en/alter-table.html)

The CHECK clause is parsed but ignored by all storage engines. See Section 13.1.17, “CREATE TABLE Syntax”. The reason for accepting but ignoring syntax clauses is for compatibility, to make it easier to port code from other SQL servers, and to run applications that create tables with references. See Section 1.8.5, “MySQL Differences from Standard

How I came to know this bad behavior of mysql is, I just created a simple table such as below and After creating I assumed the column gender_code would not accept any values other than ‘m’ or ‘f’. but unfortunately it did which pushed me in a dilemma. The bad scene is it didn’t even throw me an error.

 CREATE TABLE USER (  
 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,  
 name VARCHAR( 128 ) NOT NULL,  
 gender_code CHAR(1) NOT NULL,  
 CONSTRAINT chk_gender CHECK (gender_code = 'm' OR gender_code = 'f')  
 );  

Hence, in order to have a check constraint in mysql database I found three alternative ways.

Solution 1

Define the data type of column gender_code  as ENUM.

 ‘gender_code’ ENUM(‘m’, ‘f’) NOT NULL  

Only disadvantage i noticed in this way, when inserting any other values (not ‘m’or  ‘f’) it accepts an empty string.

Solution 2

Create a trigger to check for gender_code ‘m’ or  ‘f’ else throw an error.

 DROP TRIGGER IF EXISTS chk_gender;   
 delimiter $$  
  CREATE TRIGGER chk_gender BEFORE INSERT ON `user`   
 FOR EACH ROW  
 BEGIN  
 DECLARE msg varchar(255);  
 IF (NEW.gender <> 'm' AND NEW.gender<>'f')  
 THEN  
 SET msg = concat('Constraint chk_gender violated: gender must not contain invalid value ', cast(new.gender as char));  
 SIGNAL sqlstate '45000' SET message_text = msg;  
 END IF;  
 END$$  
 delimiter ;  

 feel this is the best way to go.

Solution 3

To create a lookup table to contain gender code (‘m’ & ‘f’) and  define current table’s gender_code as foreign key to refer lookup table.

 CREATE TABLE gender_codes  
 ( code CHAR(1) NOT NULL  
 , PRIMARY KEY (code)  
 ) ENGINE = InnoDB ;  
 INSERT INTO gender_codes (code) VALUES  (‘m’), (‘f’) ;  

And then,  original table should look like.

 CREATE TABLE user (  
 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,  
 name VARCHAR( 128 ) NOT NULL,  
 gender_code CHAR(1) NOT NULL,  
 CONSTRAINT fk_gender FOREIGN KEY (gender_code) REFERENCES gender_codes (gender_code)  
 );  

Hope these workarounds saves someone’s time.

Sunday, April 8, 2012

PHP has no FINALLY

Image result for php 5.3
I am so curious why PHP doesn’t have FINALLY block along with normal TRY CATCH blocks. It’s very much necessary and will let us stay away from code duplication. Finally is simply to do something at last after try catch block. Even if there is an exception finally will be called at last.

 class A  
 {  
   public function doSomething()  
   {  
   }  
 }  
 ...  
 ....  
 try {  
 $a->doSomething();  
 $b->status = 'success';  
 }  
 catch(exception $ex) {  
 $ex->getMessage();  
 $b->status = 'fail';  
 }  
 finally {  
 $b->save();  
 }  

Since we don’t have finally right now in php 5.4 we have to re write the above code section as following which is bit of over load work.

 try {  
 $a->doSomething();  
 $b->status = 'success';  
 $b->save();  
 }  
 catch(exception $ex) {  
 $ex->getMessage();  
 $b->status = 'fail';  
 $b->save();  
 }  

You can see i am writing the save method at two places thinking if an exception occurs. So if we have finally we may not need it this way. However as the php.net site says, Finally block has been approved now and will be pushed out with php 5.5 soon. it’s a good news and we got to stay for latest php version badly.

Friday, February 17, 2012

Make case sensitive table names in MySQL database

Image result for mysql logo

Are table names in mysql case sensitive?  The answer is no.

When you create a tabled called “User” or “USER”,  both are considered as “user” which means it’s not case sensitive.

How can we force mysql to do this ? it’s a just matter of minor change in mysql ini file.

Below given are the steps you need to follow in order to force this behavior.

1-    Open the my.ini file located inside xampp\mysql\bin folder
2-    Add this system variable to the bottom of my.ini, lower_case_table_names=2
3-    restart mysql

Reference: http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html

Tuesday, January 17, 2012

MYSQL Table locking

Image result for lock

his topic can be new to some but its very much important when working with concurrent access to a particular table row (record). for example lets say we have a inventory table which has some product details as records. now, a scenario arises like user purchases a product and we need to update its quantity. what we do is normally reduce the number of quantity by 1. but how many of us know weather it happened correctly or not ?


say a situation like this, A and B customers purchases the same product at same time.. and the initial stock count is 50, when this kinda incident occurs the mysql will not understand the Independence of both and finally will end up giving the remaining stock count as 49 where actually should be 48 since its two purchases.

mainly these things should be considered when modifying a table. (update/delete)

so as a solution we now have mysql’s internal locking mechanism. the main purpose of it is to prevent a client from reading/writing from a locked resource. in other words, if we consider the previous example.. until A finishes the purchasing and table is updated as 49 B has to wait. then after B will purchase and remaining will set as 48 which is expected.

there are two types of locking methods available.

READ LOCK – A read lock will allow the other clients to read from the locked resource but not write to it.
WRITE LOCK – a write lock will prevent the other clients from reading or writing to the locked resource.

Note: different storage engines have different lock levels. For example, MyISAM will lock the whole table while InnoDB will only lock the rows it needs.

 LOCK TABLE inventory READ; //READ LOCK  
 LOCK TABLE inventory WRITE; // WRITE LOCK  
 UNLOCK TABLES // UNLOCK  

some important notes.

WRITE locks have higher priority than READ locks
When you write a second LOCK TABLE, it will unlock all previously held locks.
To lock multiple tables, you need to lock them simultaneously. Eg: LOCK TABLE user WRITE, role READ;
reference – http://dev.mysql.com/doc/refman/5.0/en/internal-locking.html

Wednesday, December 21, 2011

PHP Incrementing & Decrementing Operators


Image result for plus and minus

What is the meaning of ++ increment operator and - – decrement operator and how can we use it ?

Below is a small explanation on what it does,

++$a means Pre-increment, It increments $a by one, then returns $a.

$a++ means Post-increment, It returns $a, then increments $a by one.

- – $a means Pre-decrement, It decrements $a by one, then returns $a.

$a – - means Post-decrement, It returns $a, then decrements $a by one.

Popular Posts

Powered by Blogger.