How to connect to an external BBDDD MS SQL

How to connect to an external BBDD MS SQL

If you want to connect from your web hosting to an external MS SQL database, you can use the mssql_connect() function.
Below we show an example code to use a connection with mssql_connect.
  
//replace the values with those of your server.
$myServer = "SERVER";
$myUser = "USER";
$myPass = "PASSWORD";
$myDB = "DATABASE";
//database connection
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
// or die("Connection failed to $myServer");
or die('MSSQL error: ' . mssql_get_last_message());
//select the database
$selected = mssql_select_db($myDB, $dbhandle)
or die("Cannot open database $myDB");
//execute the SQL query
$query = "SELECT top 1 * ";
$query .= "FROM test ";
//run the SQL query and display the results
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "
" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned
";
while($row = mssql_fetch_array($result))
{
echo "
" . $row["idVenta"] . $row["idNlinea"] . "
";
}
//close the connection
mssql_close($dbhandle);
?>

This code is used to check that the connection to the database is successfully established.


For more information, you can contact us.