PHP,APACHE@, UBUNTU and MSSQL

greggpb

Expert Member
Joined
Apr 22, 2005
Messages
1,818
Reaction score
0
Location
Cape Town
Anyone know what the recommend way is to connect from A linux based webserver to a MSSQL Database...

We have a reporting system which was running on WAMP that we are trying to move to LAMP. ??
 
Should be no difference. Just make sure your code is linux friendly - ex: no backslashes in your URL's.
 
Anyone know what the recommend way is to connect from A linux based webserver to a MSSQL Database...

We have a reporting system which was running on WAMP that we are trying to move to LAMP. ??

You probably need to have a look at the MSSQL ODBC driver for Linux. Lots of sites describing how to do this, so you probably just need to Google a bit.
 
See below how to create a variable in PHP that we decided to call "link". It is the active MySQLi connection that we will use in our queries from within our PHP scripts:

$DBHost = '127.0.0.1:3306'; // local connection on port 3306 (if you have not changed the default port). You could specify a remote MySQL server too (server running on something other than your linux box).
# $DBHost = 'localhost'; // This is essentially the same thing and it will use the default port 3306 unless otherwise specified however I have found the proceeding to be supported on various different web server setups by default whereas "localhost" has to be configured in during server setup.
$DBUser = 'mysql username'; // The user must have the appropriate privileges assigned and assigned to the database below, best not to use a root account
$DBPass = 'mysql user password';
$DBName = 'database name';

$link = mysqli_connect($DBHost, $DBUser, $DBPass);

if (!$link)
{
// Do error hand off for: Unable to connect to the database server.​
exit;​
}
if (!mysqli_set_charset($link, 'utf8'))
{
// Do error hand off for: Unable to set database connection encoding.​
exit;​
}
if (!mysqli_select_db($link, $DBName))
{
// Do error hand off for: Unable to locate this users database.​
exit;​
}

// You could test some more stuff if you're paranoid but that is a solid connection.

Example Query:

$stmt = $link->prepare("
SELECT id, name
FROM users
WHERE email = ?
AND tel = ?​
");

if (!$stmt)
{
// Do error hand off for: 'Error '.$link->errno.' : '.$link->error.'';
exit();​
}
if (!$stmt->bind_param("si", $thisUsersPredefinedEmail, $thisUsersPredefinedTelephone))
{
// Do error hand off for: 'Error '.$link->errno.' : '.$link->error.'';
exit();​
}
if (!$stmt->execute())
{
// Do error hand off for: 'Error '.$link->errno.' : '.$link->error.'';
exit();​
}

$stmt->bind_result($userId, $userName);
$stmt->fetch();
$stmt->close();

Assuming that you are locally accessing the MySQL server via the lamp on your linux system (localhost) that would work for you too.

But really... Read the manual: http://www.php.net/manual/en/book.mysqli.php
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X