Export MS SQL SERVER data in MS Excel using php



This tutorial will go over how to download MS SQL Server data into Excel file. This is Very useful for generating Excel Reports of Php and MS SQL Server Applications. 

<?php
$myServer = "host_name";
$myUser = "user_name";
$myPass = "password";
$myDB =  "database_name";

//create an instance of the  ADO connection object
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");

//define connection string, specify database driver
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr); //Open the connection to the database

//declare the SQL statement that will query the database
$query = "SELECT col1, col2, col3, .... , coln FROM table_name";

//execute the SQL statement and return records
$rs = $conn->execute($query);

$num_columns = $rs->Fields->Count();
//echo "col:".$num_columns . "<br>";

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

$contents="<table border='1'>";

while (!$rs->EOF)  //carry on looping through while there are records
{
    $contents.="<tr>";
    for ($i=0; $i < $num_columns; $i++) {
        $contents.="<td>" . $fld[$i]->value . "</td>";
    }
    $contents.="</tr>";
    $rs->MoveNext(); //move on to the next record
}

$contents.="</table>";

$file="File_name.xls";
$test="<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=File_name".date('Y-m-d').".xls");
echo $contents;

//close the connection and recordset objects freeing up resources
$rs->Close();
$conn->Close();

$rs = null;
$conn = null;
?>

2 comments :

Display MS Sql Server Data using PHP



This tutorial will go over how to Display MS SQL Server datausing PHP.

<?php
$myServer = "host_name";
$myUser = "user_name";
$myPass = "password";
$myDB =  "database_name";

//create an instance of the  ADO connection object
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");

//define connection string, specify database driver
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr); //Open the connection to the database

//declare the SQL statement that will query the database
$query = "SELECT col1, col2, col3, .... , coln FROM table_name";

//execute the SQL statement and return records
$rs = $conn->execute($query);

$num_columns = $rs->Fields->Count();
//echo "col:".$num_columns . "<br>";

for ($i=0; $i < $num_columns; $i++)
{
    $fld[$i] = $rs->Fields($i);
}

echo "<table>";

while (!$rs->EOF)  //carry on looping through while there are records
{
    echo "<tr>";
    for ($i=0; $i < $num_columns; $i++) {
        echo "<td>" . $fld[$i]->value . "</td>";
    }
    echo "</tr>";
    $rs->MoveNext(); //move on to the next record
}

echo "</table>";

//close the connection and recordset objects freeing up resources
$rs->Close();
$conn->Close();

$rs = null;
$conn = null;
?>

0 comments :

Add a Pinterest button to site





Another Simple Tutorial to Add a Pinterest button to site.

Pins are like little bookmarks. Whenever you find something on the web that you want to keep, add it to Pinterest. If your site have multiple images and you want give facility to users to bookmark this images for later use. you can use this Pinterest plugin.

Steps to Add Pinterest Button to site.

1) Add following pinterest script to your site.

<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>

2) Create pin it button using simple Anchor Tag.

<a href="//www.pinterest.com/pin/create/button/?url=<SITE_URL>&media=<IMAGE_TO_PIN_IT>&description=<DESCRIPTION_TO_SHARE>" data-pin-do="buttonPin" data-pin-config="above"  target="_blank"><img src="images/btn_pinit.png" class="shares" ></a>

Its done. :)

for more information visit:- http://business.pinterest.com/widget-builder/#do_pin_it_button

Note :- You can use encodeURIComponent('Text To Encode') javascript function to encode text into url format.

0 comments :

Send Facebook Notification via Graph API using PHP



function fbnotification($userid,$message)
{
$app_id = "<APPLICATION_ID>";
$app_secret = "<APPLICATION_SECRET_KEY>";
$app_access_token = $app_id . '|' . $app_secret;

$facebook = new Facebook(array(
'appId'  => $app_id,
'secret' => $app_secret,
'cookie' => true,
));

$response = $facebook->api( '/'.$userid.'/notifications', 'POST', array(
'template' => $message,
'access_token' => $app_access_token
) );  
}

fbnotification($userid,$message)

0 comments :