Connection Function/Class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markyboy17
Forum Newbie
Posts: 7
Joined: Mon Jan 16, 2012 3:38 am

Connection Function/Class

Post by markyboy17 »

Below is how i am connecting to my database. I do this on every page.

Code: Select all

 
$cdatabase = 'XXXXXXX;
$cUID = 'XXXXXX';
$cPWD = 'XXXXXX';
$cSRVname = 'XXXXXXX';
$cconnectionInfo = array("Database" => $cdatabase, "UID" => $cUID, "PWD" => $cPWD);
$cconn = sqlsrv_connect($cSRVname, $cconnectionInfo);
My question is how do make this into a function of some sorts. And how do i call it on each page

BTW i will be using the variable $cconn when i do queries and things how would i make it so this variable is accessible to use on every page (i.e Make it a global variable)

Thanks in advance
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Connection Function/Class

Post by Celauran »

Rather than creating a new class, use the excellent PDO.
Post Reply