Types of KEY in MySQL

 

KEY NAMES KEY DESCRIPTION
PRIMARY KEY Name of the column which uniquely identifies each row of the table with no null value in any row of the table.
UNIQUE KEY Name of the column which uniquely identifies each row of the table, but it can accept a null value for any row of the table.
COMPOSITE KEY The combination of two or more column representing as a unique for each row of the table after merging the columns. It acts like a single column in contest of the key.
SURROGATE KEY A key which has no buisness logic or which have no existance in the real world. for more clarity see NATURAL KEY
FOREIGN KEY Name of the column which have the reference of the other table by having the same value of any column other table.
CANDIDATE KEY Any key which can represent as unique for each row is called CANDIDATE KEY. Hence PRIMARY KEY, UNIQUE KEY and COMPOSITE KEY are a member of CANDIDATE KEY
NATURAL KEY A key which have a real existinence in business logic or have the real value in the world like AADHAR NO in India and SSN NO in USA 
   

Make a chess using PHP

  1. <?php
  2. echo '<table border="1" cellpadding="0" cellspacing="0">';
  3. $black = true;
  4. for ($i = 1; $i <= 8; $i++) {
  5. echo "<tr>";
  6. for ($j = 1; $j <= 8; $j++) {
  7. if ($black == true)
  8. echo "<td style='background: #000;height: 30px;width: 30px;'>&nbsp;</td>";
  9. else
  10. echo "<td style='background: #fff;height: 30px;width: 30px;'>&nbsp;</td>";
  11. $black = !$black;
  12. }
  13. $black = !$black;
  14. echo "</tr>";
  15. }
  16. echo "</table>";
  17. ?>

Output:

chess

 

Find Armstrong Number in PHP

  1. <?php
  2. function is_armstrong($digits)
  3. {
  4. $digits_arr = str_split($digits);
  5. $cube_digit = 0;
  6. foreach ($digits_arr as $digit) {
  7. $cube_digit += find_cube($digit);
  8. }
  9. if ($cube_digit == $digits)
  10. return true;
  11. else
  12. return false;
  13. }
  14.  
  15. function find_cube($no)
  16. {
  17. return $no * $no * $no;
  18. }
  19.  
  20. var_dump(is_armstrong(373));
  21.  
  22. ?>

or

  1. <?php
  2.  
  3. function is_armstrong($number)
  4. {
  5. $num = $number;
  6. $arms = 0;
  7. while ($number > 1) {
  8. $temp = $number % 10;
  9. $arms = $arms + ($temp * $temp * $temp);
  10. $number = $number / 10;
  11. }
  12. if ($num == $arms)
  13. return true;
  14. else
  15. return false;
  16. }
  17.  
  18. var_dump(is_armstrong(163));
  19. ?>

 

Store all data in an array to remove duplicate record when using LEFT JOIN in MySql

Sometime you’d stuck like that you’ll need all other rows coming with left join as a duplicate row for all record, where you would need to have all these inside an array variable. For example if you have an tbl_emp and another tbl_contact, in tbl_contact you would have one foreign key emp_id.

You made a query to fetch all employee record with their contact nos, when you left join the query such like LEFT JOIN tbl_contact ON tbl_contact.emp_id=tbl_emp.emp_id, you will get record in associative array something like this.

array (size=3)
  0 => 
    array (size=3)
      'name' => string 'test1' (length=5)
      'age' => int 23
      'email' => string 'Good' (length=4)
  1 => 
    array (size=3)
      'name' => string 'test2' (length=5)
      'age' => int 24
      'email' => string 'Good' (length=4)
  2 => 
    array (size=3)
      'name' => string 'test2' (length=5)
      'age' => int 24
      'email' => string 'Best' (length=4)

 Here you are getting two record for test2 employee, because this employee has two email as a contact.

So, to simplify this you will need to have all contact nos inside a single variable key and the duplicate record to be removed, then here is the logic to do that.

 

 

  1. <?php
  2. $total_size=sizeof($arr);
  3. for($i=0;$i<$total_size;$i++){
  4. $arr[$i]['emails']=array();
  5. $arr[$i]['emails'][]=$arr[$i]['email'];
  6. $unset=0;
  7. foreach($arr[$i] as $key=>$value){
  8. if($key=='name' && $arr[$i]['name']==$arr[$i+1]['name']){
  9. $arr[$i]['emails'][]=$arr[$i+1]['email'];
  10. $unset=1;
  11. }
  12. }
  13. unset($arr[$i]['email']);
  14. if($unset==1){
  15. unset($arr[$i+1]);
  16. $i++;
  17. }
  18. }
  19. var_dump($arr);
  20. ?>

 

 

Now you’ll get the result something like that

array (size=2)
  0 => 
    array (size=3)
      'name' => string 'test1' (length=5)
      'age' => int 23
      'emails' => 
        array (size=1)
          0 => string 'Good' (length=4)
  1 => 
    array (size=3)
      'name' => string 'test2' (length=5)
      'age' => int 24
      'emails' => 
        array (size=2)
          0 => string 'Good' (length=4)
          1 => string 'Best' (length=4)