Author Archives: Yuvaraj Jeganathan

About Yuvaraj Jeganathan

Am a web developer and designer,

How to Connect MSSQL with php using PDO-SQLSRV

PHP is Supporting Many Databases. We can connect MSSQL with PHP using the following method. There are many different ways available to connect MSSQL with PHP, here is the tutorial for establishing database connectivity between MSSQL and PHP using PDO.

Step 1: Install the SQL Server related version you need from this link. Click here

Step 2: Install MSSQL Server with developer and change mode of Authentication using mixed SQL Server authentication and put your password there.

Step3: After installing MSSQL add your database and tables.

Step 4: Next you need the SQL Server connecting driver for php this available at here    sqlsrv30.exe

Step 5: After downloading sqlsrv30.exe, extract the file and there will be following files.

MSSQL Connect with php server driver uploading

MSSQL Conncet with php and selecting extension for depending on your db

Step 6: Check your php version using phpinfo(); function and choose two files depending on your version.

  • If your version is 5.3, then copy the following files php_pdo_sqlsrv_53_ts.dll & php_sqlsrv_53_ts.dll  and paste into   Drive Installed:wamp/bin/php/phpversion/ext and paste the files.
  • If your version is 5.4, then copy the following files php_pdo_sqlsrv_54_ts.dll & php_sqlsrv_54_ts.dll  and paste into   Drive Installed:wamp/bin/php/phpversion/ext and paste the files. 

step 7: After  Pasting these two files you need to change the php.ini(wamp\bin\apache\apache2.2.22\bin) file add this line extension=php_sqlsrv_53_ts.dll,
extension=php_pdo_sqlsrv_ts.dll and extension=php_pdo_sqlsrv_53_ts.dll

Mssq connect with php  changing extension

Extension adding for mssql connect

step 8: Now save the php.ini file and restart the wamp server

step 9: PDO to connect with database code given below

[code type=php ]

try
{
$serverName=”SERVER-Name”;//
$conn = new PDO( “sqlsrv:server=$serverName ; Database=db_name”, server-name”, “your_password”);
}

catch(Exception $e)
{
echo $e.”Error in connect with mssql”;
}

[/code]

step 10: If you are using mssql version below 2012 you need to install sql server native client for 2012 download here.

(Find the name sqlncli.msi with microsoft version download and install)

 

Category: PHP

Calculating Difference Between Two days And Display Particular Period of days Using PHP

In PHP some application depends on date , Calculating Diffence between two days and displaying a period of days or month or years.

We Are using to calculate difference between two days using DateTime class. This helpfull for many web application that depends on date time based.

Finding Difference between  two days:

[code]

$date1 = new DateTime(’12-09-2014′);
$date2 = new DateTime(’23-09-2014′);
$interval = $date1->diff($date2);
echo $interval->format(‘%a’);

[/code]

This Example will gives output 11 for the difference. diff() function used to find the difference from two days

Prininting period of days:

In that example period of days will shows class DateInterval();

And here used modify function which modify what we give inside function plus(+) or minus(-) days, months and year example $date->modify(“+2 day”) is add two days from its original date $date->modify(“-2 month”) reduce two months from original date.

We use the value P1D means  1 day interval we can change the day interval depend on our needs

Same as  Value P1M means 1 month interval and year for P1Y

 

[code]

$begin = new DateTime( ‘2012-08-01’ );
$end = new DateTime(‘2012-08-01’);
$end = $end->modify( ‘+100 day’ );
echo “<b>Date</b><br>”;
$interval = new DateInterval(‘P1D’);
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
echo $date->format(“Y-m-d”) . “&nbsp; &nbsp;”;
}
$end = $end->modify( ‘+25 month’ );
$interval = new DateInterval(‘P1M’);
$daterange = new DatePeriod($begin, $interval ,$end);
echo “<br><b>Months</b>”;
foreach($daterange as $date){
echo “<br><b>”.$date->format(“Y-m-d (D)”) . “<b>”;
}
$end = $end->modify( ‘+2 year’ );
$interval = new DateInterval(‘P1Y’);
$daterange = new DatePeriod($begin, $interval ,$end);
echo “<br><b>Years</b>”;
foreach($daterange as $date){
echo “<br><strong style=’color:red’>”.$date->format(“Y-m-d (D)”) . “<strong>”;
}

[/code]

Here the modify function works add days or month or year

This code Out puts

Date
2012-08-01   2012-08-02   2012-08-03   2012-08-04   2012-08-05   2012-08-06   2012-08-07   2012-08-08   2012-08-09   2012-08-10   2012-08-11   2012-08-12   2012-08-13   2012-08-14   2012-08-15   2012-08-16   2012-08-17   2012-08-18   2012-08-19   2012-08-20
Months
2012-08-01 (Wed)
2012-09-01 (Sat)
2012-10-01 (Mon)
2012-11-01 (Thu)
2012-12-01 (Sat)
2013-01-01 (Tue)
2013-02-01 (Fri)
2013-03-01 (Fri)
2013-04-01 (Mon)
2013-05-01 (Wed)
2013-06-01 (Sat)
2013-07-01 (Mon)
2013-08-01 (Thu)
2013-09-01 (Sun)
2013-10-01 (Tue)
2013-11-01 (Fri)
2013-12-01 (Sun)
2014-01-01 (Wed)
2014-02-01 (Sat)
2014-03-01 (Sat)
2014-04-01 (Tue)
2014-05-01 (Thu)
2014-06-01 (Sun)
2014-07-01 (Tue)
2014-08-01 (Fri)
2014-09-01 (Mon)

Years
2012-08-01 (Wed)
2013-08-01 (Thu)
2014-08-01 (Fri)
2015-08-01 (Sat)
2016-08-01 (Mon)