How to Connect MS SQL Database with PHP Using XAMPP

database-designPHP

I am trying to connect MSsql database and php using xamp. But i am unable to connect. bellow is my code. Please help me to find this.

<?php
$myServer = "x.x.x.x";
$myUser = "xxx";
$myPass = "xxxx";
$myDB = "xxxx";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned     </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);

Output error msg is:

Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\roshid\mssqlphp.php on line 8

Best Answer

You don't have the MS SQL Drivers installed. You can check this with phpinfo();

On Linux you need mssql.so or sybase.so With debian its apt-get install php5-sybase

For windows take a look here: http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx

Drivers need to be configured for PHP to find the function mssql_...

You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed.

Related Question