Skip to main content

Posts

Showing posts from May, 2014

MySql join

INNER JOIN (or just JOIN) The most frequently used clause is INNER JOIN. This produces a set of records which match in both the user and course tables, i.e. all users who are enrolled on a course: SELECT user.name, course.name FROM `user`INNER JOIN `course` on user.course = course.id; Result: user.name course.name Alice HTML5 Bob HTML5 Carline CSS3 David MySQL LEFT JOIN What if we require a list of all students and their courses even if they’re not enrolled on one? A LEFT JOIN produces a set of records which matches every entry in the left table (user) regardless of any matching entry in the right table (course): SELECT user.name, course.name FROM `user` LEFT JOIN `course` on user.course = course.id; Result: user.name course.name Alice HTML5 Bob HTML5 Emma (NULL) RIGHT JOIN Perhaps we require a list all courses and students even if no one has been enrolled? A RIGHT JOIN produces a set of records which matches every entry in the right table (course) regardless of any matching entry in ...

MySql Storage Engine

Change table engine to MyISAM ALTER TABLE `tableName` ENGINE = MYISAM Change table engine to innodb ALTER TABLE `tableName` ENGINE = innodb How to create table to MYISAM mysql> CREATE TABLE table2 (col1 INT, col2 CHAR(30)) ENGINE = MYISAM; Features of MYISAM and INNODB Feature InnoDB MyISAM Storage limits 64TB 256TB Transactions Yes No Locking granularity Row Table MVCC Yes No B-tree indexes Yes Yes Clustered indexes Yes No Data caches Yes No MyISAM MyISAM doesn’t support foreign key constraints or transactions , which are essential for data integrity. Hence we call MySQL with MYISAM is DBMS MYISAM not supports transaction. You cannot commit and rollback with MYISAM. MYISAM supports Table-level Locking : In addition, the whole table is locked whenever a record is inserted or updated; this causes a detrimental...

Simple Example of PHP Array Function

Simple Example of PHP Array Function Predefined  functions: 1: sizeof(): This function returns the number of elements in an array. Eg:   $data = array("red", "green", "blue"); echo "Array has " . sizeof($data) . " elements"; Output: Array has 3 elements 2: array_values($arr): This function accepts a PHP array and returns a new array containing only its values (not its keys). Eg :$data = array("hero" => "Holmes", "villain" => "Moriarty"); print_r(array_values($data)); Output: Array ( [0] => Holmes,[1] => Moriarty) 3: array_pop($arr): This function removes an element from the end of an array. $data = array("Donald", "Jim", "Tom"); array_pop($data); print_r($data); ?>  Output: Array( [0] => Donald [1] => Jim ) 4: array_push($arr, $val): This function adds an element to the end of an array. $data = array("Don...

Php sorting algorithm

Bubble Sort function bubbleSort(array $arr)     { $n = sizeof($arr); for ($i = 1; $i < $n; $i++) { for ($j = $n - 1; $j >= $i; $j--) { if($arr[$j-1] > $arr[$j]) { $tmp = $arr[$j - 1]; $arr[$j - 1] = $arr[$j]; $arr[$j] = $tmp; } } }   return $arr;    }    $arr = array(255,1,22,3,45,5); $result = bubbleSort($arr); print_r($result); ---------------------------------------------x---------------------------------------------- Selection Sort function selectionSort(array $arr) {     $n = sizeof($arr);     for ($i = 0; $i < $n; $i++) {         $lowestValueIndex = $i;         $lowestValue = $arr[$i];         for ($j = $i + 1; $j < $n; $j++) {             if ($arr[$j] < $lowestValue) {                 $lowestValueIndex = $j; ...