Wednesday, 8 April 2015

PHP MySQL interview questions and answers for experienced

PHP MySQL interview questions and answers for experienced




Question: What are 3 different ways to connect  with MySQL?
Following are 3 API with which we can connect to MySQL
1. MySQL
2. MySQLI
3. PDO

We should use MySQLI because MySQLI is imporoved version of MySQL and it have lot of functions which are absent in MySQL.


Question: What is the best collation to use for MySQL?
utf8_general_ci: It is used for fast sorting but it is less accurate.
utf8_unicode_ci: It is used for better accuracy but little slow as compare to utf8_general_ci.

You can also use for specific languages like utf8_swedish_ci.


Question: Give an example of Transaction with commit and rollback?
try {
    // First of all, let's begin a transaction
    $db->beginTransaction();

    // A set of queries; if one fails, an exception should be thrown which we are handling at bottom
    $db->query('1 query exe');
    $db->query('2 query exe');
    $db->query('3 query exe');
    
    $db->commit();
} catch (Exception $e) {
    // An exception has been thrown by try
    //This function have all the things which you want to rollback.

    $db->rollback();
}


Question: How to use "MySQL in clause" for "string search" and "number search"?
When we are searching with number: We need not to add single quote
$sql=mysql_query('SELECT * FROM `users` WHERE id IN ( 1, 2, 34 )');
while($data = mysql_fetch_array($sql)){
    //write here your code
}
When we are searching with string:Its compulsory to add single OR double quote.
$sql=mysql_query("SELECT * FROM `users` WHERE email in('myemail@gmail.com','anotheremail@gmail.com','testemail@gmail.com')");
while($data = mysql_fetch_array($sql)){
    //write here your code
}


Question: How to get the closed 10 locations between with lat long?
You can try below MySQL as its working for me:
$sql='SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) 
* cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance 
FROM city HAVING distance < 25 ORDER BY distance asc limit 0,10';

while($data = mysql_fetch_array($sql)){
    //write here your code
}



Question: How to use update query based on SELECT Query?
You have to use the join for this, See below format:
update tab1
left join table2 tab2 on
    tab1.name = tab2.name
set
    update_field = if(start_dates > end_dats, 'valid', 'Not-valie')



Queston: How to optimize table?
OPTIMIZE TABLE `users` 



Question: How to repair table?
you can use any of below:
REPAIR TABLE users
REPAIR TABLE users QUICK
REPAIR TABLE users EXTENDED






Tuesday, 7 April 2015

jQuery Interview Questions and Answers for 2 Year experienced

jQuery Interview Questions and Answers for experienced


Question: How to select all the images of a div tag?
Use below of code snippets
$('div.mydiv').find('img');
OR
$('div.mydiv').children('img');


Question: How to get the value of selected checkbox?
Get the radio button value by button NAME
$("input[name='radioButtonName']:checked").val();
Get the radio button value by button CLASS
$('.radioButtonClass:checked').val();


Question: How to get the value of selected checkbox?
Get the checkbox value by NAME
$("input[name='checkboxName']:checked").val();
Get the checkbox value by CLASS
$('.chekboxClass:checked').val();


Question: How to check, If element exist OR not?
Check the existence of element By ID
if($('#mydivId').length )){
    //console.log('Element exist);
}else{
//console.log('Element does exist);
} 

Check the existence of element By CLASS
if($('.mydivClass').length )){
    //console.log('Element exist);
}else{
//console.log('Element does exist);
} 


Question: How to select elements which have two classes?
Just write the selectors together without spaces, to select the elements having two classes?
myDivClass1$('#mydivId.myDivClass1.c.myDivClass2').val();
It will get all the elements, which have div with mydivId (ID), div with myDivClass1 (Class) and div with myDivClass2(class).


Question: How to disabled a button?
Disabled the button by ID
$("input#myDivId").prop('disabled', true);

Disabled the button by CLASS
$("input#myDivClass").prop('disabled', true);


Question: How to change the anchor tag's href?
Change the href value by ID
$("a#myATagId").attr("href", "http://www.web-technology-experts-notes.in/p/sitemap.html");
Change the href value by CLASS
$("a.myATagClass").attr("href", "http://www.web-technology-experts-notes.in/p/sitemap.html")


Question: How to set the cookie with jQuery?
jQuery core do not have cookie functions. For this, we can include jQuery cookie plugins and its really make the cookie handling very easy.

Download Cookie plugin from Below URLs.
http://plugins.jquery.com/cookie/
Set the cookie using plugin.
$.cookie("cookieName", "cookieValue", { path: '/', expires: 7 }); 

Get a cookie using plugin.
$.cookie("cookieName");

Delete the cookie.
$.removeCookie("cookieName");



Question: How to update the HTML of container?
Update the container with using ID
$('div#myDivId').html('this is new updated html tag.');

Update the container with using class
$('div.myDivClass').html('this is new updated html tag.');



Question: How to get the HTML of container?
Get the container's value with using ID
$('div#myDivId').html();

Get the container's value with using class
$('div.myDivClass').html();



Question: How to distinguish between left and right mouse click with jQuery on paragraph tag?
$('p#myParagraphId').mousedown(function(event) {
    switch (event.which) {
        case 1:
            //alert('Left Mouse.');
            break;
        case 2:
            //alert('Middle Mouse .');
            break;
        case 3:
           // alert('Right Mouse .');
            break;
        default:
            //alert('Else click');
    }
});


Question: How to get the hash values of URL?
As we know, we can't get the hash values with using PHP. but we can get the hash values with javaScript.
if(window.location.hash) {
  // hash values
}



Question: How to change the image source using jQuery?
Change the image source with using ID
$('img#myImageId').attr('src','http://mydomain.com/newimage.png');

Change the image source with using Class
$('img.myImageClass').attr('src','http://mydomain.com/newimage.png');