php-mysql-Export to excel sheet

$host = ‘localhost’;
$user = ‘root’;
$pass = ”;
$db = ‘testexport’;
$table = ‘testdata’;
$file = ‘export’;

$filename = $file.”_”.date(“Y-m-d”);

$link = mysql_connect($host, $user, $pass) or die(“Can not connect.” . mysql_error());
mysql_select_db($db) or die(“Can not connect.”);

$result = mysql_query(“SHOW COLUMNS FROM “.$table.””);
$i = 0;$csv_output=”;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row[‘Field’].”, “;
$csv_output .= “\n”;

$values = mysql_query(“SELECT * FROM “.$table.””);
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].”, “;
$csv_output .= “\n”;

$filename = $file.”_”.date(“Y-m-d_H-i”,time());
header(“Content-type: application/”);
header(“Content-disposition: csv” . date(“Y-m-d”) . “.csv”);
header( “Content-disposition: filename=”.$filename.”.csv”);
print $csv_output;

very simple right ūüėČ


How to post the data in php using ajax??

Hey guys,

Once i got a situation to use iframe where i need to post the data of an employee in the same page using iframe(display all details of that employee). I couldn’t understand how to proceed..but i came to know that using ajax we can do that..

here is the scenario:

I need to click on a employee so that an iframe will open with his details..if i click another employee name then second employee details should be displayed in same iframe.

echo”<a href=’javascript:void(0);’ role=’button’ ¬†class=’empclick’ alt='”.$role[$i].”‘ onclick=’getElementValue(“.$role[$i].”,”.$Empid[$i].”,”. $status[$i].”)’>”;/*echo $role[$i];*/ echo $name[$i]; echo”</a>”;


function getElementValue(role,id,status)

type: “POST”,
url: “http://localhost:82/projectname/controllername/AssiningSession/”+ role+”/”+id+”/”+status ,
data: { value: role }
}).done(function( msg ) {
// alert( “Data Saved: ” + msg );

This helped me alot..hope it helps you too ūüôā

What’s the difference between POST and raw POST in PHP

Before going to description..first just check this link at glance

We can divide form submissions in three cases:

  1. Submissions with content type application/x-www-form-urlencoded
  2. Submissions with content type multipart/form-data
  3. Other submissions.

In cases 1 and 3, $HTTP_RAW_POST_DATA contains the raw post data (except if the option isalways_populate_raw_post_data is set to false, in which case $HTTP_RAW_POST_DATA is empty in case 1), i.e., the data exactly as the client (usually the browser) has sent it. In case, 1, the data has a form such as


PHP automatically parses this, so that $_POST becomes:

$_POST = array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => array("value3.1", "value3.2");

The contents of the raw data can also be access through php://input, even in case 1 whenalways_populate_raw_post_data is set to false. In particular,file_get_contents("php://input") gives the same data $HTTP_RAW_POST_DATA has or would have.

In case 3, in which the POST data is arbitrary, $_POST will be an empty array and$HTTP_RAW_POST_DATA will always be populated.

Case 2 is a special one. In that case, PHP will parse the data and $_POST will get the content of the fields which are not uploaded files, but php://input and $HTTP_RAW_POST_DATA will be unavailable.

To get the Raw Post Data:

<?php $postdata¬†=¬†file_get_contents(“php://input”);¬†?>

PHP to XML and viceversa Conversions

PHP -> XML :

PHP File:


//Creates XML string and XML document using the DOM
$dom = new DomDocument(‘1.0’);

//add root – <data>
$data = $dom->appendChild($dom->createElement(‘DATA’));

//add <category> element to <data>
$category = $data->appendChild($dom->createElement(‘category’));


//generate xml 

$dom->formatOutput = true; // set the formatOutput attribute of
// domDocument to true
// save XML as string or file
$test1 = $dom->saveXML(); // put string in test1
$dom->save(‘test1.xml’); // save as file

text1.xml output:

<?xml version=”1.0″?>


$data_array = (array) simplexml_load_string($string);

foreach ($data_array as $field => $value) {

echo “$field => $value “;