Home
>
PHP > Database Connections in PHP
Database Connections in PHP
1.1) MYSQL Connection in PHP
We can use mysql_connect and mysql_select_db functions to create our connections. For closing the connections there is mysql_close function. These are basic functions of managing connections in PHP. There are also a lot of classes made by developers for free.
Sturcture 1.0:
1 2 3 4 5
| <?php
$ConnectionName = mysql_connect('MyHost', 'MyLogin', 'MyPassword');
$DatabaseConnection = mysql_select_db('MyDatabaseName', $ConnectionName);
mysql_close($ConnectionName);
?> |
You can determine the
MyHost,
MyLogin,
MyPassword and
MyDatabaseName variables as you need.
Example 1.0: We used hostname:port syntax to make our connection.
1 2 3 4 5 6 7 8 9 10 11 12
| <?php
$my_conn = mysql_connect('memiso.com:3307', 'MemisoLogin', 'MemisoPass');
if (!$my_conn) {
die('Error Occurred: ' . mysql_error());
}
$db_conn = mysql_select_db('MemisoDB', $my_conn);
if (!$db_conn) {
die ("Couldn't Connected MemisoDB. Error Occurred: " . mysql_error());
}
mysql_close($my_conn);
?> |
Example 1.1: There is also this usage for checking connection status and getting Mysql errors.
1 2 3 4 5 6
| <?php
@mysql_connect('localhost', 'MemisoLogin', 'MemisoPass') or die('Error Occurred: ' . mysql_error());
@mysql_select_db('MemisoDB', $my_conn) or die ('Couldn\'t Connected MemisoDB. Error Occurred: ' . mysql_error());
mysql_close($my_conn);
?> |
1.2) MSSQL (Microsoft SQL Server) Connection via ODBC in PHP
We can connect to SQL 7, 2000, 2005 and 2008 with SQL Native Client 10.0 ODBC Driver.
Sturcture 2.0: Creating and closing a MSSQL connection in PHP.
1 2 3 4 5
| <?php
$ConnectionName = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$MyHostName;Database=$MyDatabaseName;", $MyUserName, $MyPassword);
odbc_close($ConnectionName);
?> |
You can determine the values of
MyHostName,
MyDatabaseName,
MyUserName and
MyPassword variables as you need.
Example 2.0:
1 2 3 4 5
| <?php
$mssql_conn = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$localhost;Database=$mssqldb;", $memisologin, $memiso_pass);
odbc_close($mssql_conn);
?> |
1.3) MDB (Microsoft Access Database) Connection via ODBC in PHP
Sturcture 3.0: Creating and closing a MDB connection in PHP.
1 2 3 4 5
| <?php
$ConnectionName = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$MyMDBFile", $MyUserName, $MyPassword);
odbc_close($ConnectionName);
?> |
You can determine the values of
MyMDBFile,
MyUserName and
MyPassword variables as you need.
Example 3.0:
1 2 3 4 5 6
| <?php
$MyMDBFile = "../db/memisodb.mdb";
$mdb_conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$MyMDBFile", '', '');
odbc_close($mdb_conn);
?> |
Example 3.1:
1 2 3 4 5
| <?php
$mdb_conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=memiso.db", '', '');
odbc_close($mdb_conn);
?> |
1.4 XLS (Microsoft Excel) Connection via ODBC in PHP
Sturcture 4.0: Creating and closing a MDB connection in PHP.
1 2 3 4 5 6
| <?php
$FileName = realpath('MyFileRoot');
$FileDir = dirname($FileName);
$ConnectionName = odbc_connect("Driver={Microsoft Excel Driver (*.xls)}; DriverId=790;Dbq=$FileName;DefaultDir=$FileDir" , '', '');
odbc_close($ConnectionName);
?> |
You can determine the values of
FileName,
MyFileRoot,
FileDir variables as you need.
Example 4.0:
1 2 3 4 5
| <?php
$excel_file = realpath('C:/MyFile.xls');
$myfile_dir = dirname($excel_file);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)}; DriverId=790;Dbq=$excel_file;DefaultDir=$myfile_dir" , '', '');
?> |
Thx for this nice overview.
Gonna use this (with a note to this site) as a learning instruction for my co-workers.
regards,
banana