Monday, 7 September 2015

How to Detect Request type in PHP

How to Detect Request type (GET OR Post ) in PHP

Question:How to Detect Request type (Get OR Post) in PHP?
$method = $_SERVER['REQUEST_METHOD'];
try {
    switch ($method) {
        case 'GET':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'POST':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'PUT':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'HEAD':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'DELETE':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'OPTIONS':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        default:
            echo "$method method not defined";
            break;
    }
} catch (Exception $e) {
    echo $e->getMessage();
}



Question: How to get all post data in PHP?
$postData = array();
if(!empty($_POST)){
    $postData = $_POST;
}
print_r($postData );



Question: How to get all GET data in PHP?
$getData = array();
if(!empty($_GET)){
    $getData = $_GET;
}
print_r($getData );



Question: How to get Document root path in PHP?
echo $_SERVER['DOCUMENT_ROOT'];



Question: How to get Client Ip-Address in PHP?
echo $_SERVER['REMOTE_ADDR'];



Wednesday, 2 September 2015

How to export mysql query results to csv?

How to export mysql query results to csv?

To Learn OR understand the exporting mysql results in CSV.
Lets have below simple example.


  1. Step 1: Create a employee table
    CREATE TABLE IF NOT EXISTS `employee` (
      `id` int(11) unsigned NOT NULL,
      `first_name` varchar(100) DEFAULT NULL,
      `last_name` varchar(100) DEFAULT NULL,
      `gender` enum('m','f') DEFAULT NULL,
      `status` enum('0','1') NOT NULL COMMENT '0-Inactive, 1-Active',
      `address` varchar(255) DEFAULT NULL,
      `created_at` datetime DEFAULT NULL,
      `modified_at` datetime DEFAULT NULL
    ) ENGINE=InnoDB AUTO_INCREMENT=6;

  2. Add The records in employee table
    INSERT INTO `employee` (`id`, `first_name`, `last_name`, `gender`, `status`, `address`, `created_at`, `modified_at`) VALUES
    (1, 'Sunil', 'Malhotra', 'm', '1', '3020 new town, chandigarh', '2015-09-02 12:38:00', '2015-09-02 12:38:00'),
    (2, 'Anil ', 'Yadav', 'm', '1', '3020 new town, chandigarh', '2015-09-02 12:38:00', '2015-09-02 12:38:00'),
    (3, 'Aman ', 'Verma', 'm', '1', '3020 new town, chandigarh', '2015-09-02 12:38:00', '2015-09-02 12:38:00'),
    (4, 'Ram ', 'Singh', 'm', '1', '3020 new town, chandigarh', '2015-09-02 12:38:00', '2015-09-02 12:38:00'),
    (5, 'Arun', 'Kumar', 'm', '1', '3020 new town, chandigarh', '2015-09-02 12:38:00', '2015-09-02 12:38:00');
    

  3. Create a folder where you are going to export the csv file.
  4. Execute the Mysql Query.
    SELECT id,first_name,last_name,gender, STATUS, address FROM employee
    INTO OUTFILE 'E:/wamp/www/export/employee3.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    LINES TERMINATED BY '\n';