-1

I can not recognize the SQL Server command in XAMPP 1.8.1 and PHP 5.4.7, this code had already used it and it worked before but now it doesn't.

I have already tried to put the php_mssql.dll dll and still don't recognize the command:

$con = mssql_connect('187.164.1.2/base','pag','123') or die('Could not connect to the server!');
mssql_select_db('aguacom') or die('Could not select a database.');

I expected it to connect to the other server's database.

Zhorov
  • 28,486
  • 6
  • 27
  • 52

2 Answers2

1

You are trying to connect to MS SQL Server using MSSQL PHP extension (mssql_ functions), but this extension is not available anymore on Windows with PHP 5.3 and removed in PHP 7.0.0.

What you can do is to install PHP Driver for SQL Server (sqlsrv_ functions). You need to download:

  • The appropriate version of this driver - for PHP 5.4 you need version 3.2 (32-bit or 64-bit depending on the PHP version)
  • The appropriate ODBC driver.

Note, that MSSQL PHP extension (php_mssql.dll) and PHP Driver for SQL Server (php_sqlsrv_54_ts.dll) are two different PHP extensions.

Example using mssql_ functions:

<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";

$conn = mssql_connect($server, $username, $password);
if ($conn === false) {
    echo "Unable to connect. ".mssql_get_last_message()."</br>";
    exit;
} else {
    echo "Connected.</br>";
}
mssql_select_db($database, $conn);

// ...

mssql_close($conn);
?>

Example using sqlsrv_ functions:

<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";

$connectionInfo = array(
    "UID" => $username,
    "PWD" => $password,
    "Database" => $database
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
    echo "Unable to connect. ".print_r(sqlsrv_errors(), true)."</br>";
    exit;
} else {
    echo "Connected.</br>";
}

// ...

sqlsrv_close($conn);
?>
Zhorov
  • 28,486
  • 6
  • 27
  • 52
0

you should enable mssql in xampp

things you need [download these files]: https://www.microsoft.com/en-us/download/details.aspx?id=20098

Add downloaded files in php.ini like this way

extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll

restart apache and then check

<?php
   echo "<pre>";
   print_r(PDO::getAvailableDrivers()); ?>

credits goes to rray

Balaji
  • 9,657
  • 5
  • 47
  • 47
  • but do I need version 56 or 54 ?, because I have php 5.4.7 – JUANPABLODEJESUS FIGUEROA JARA Sep 07 '19 at 02:52
  • it does support Version 4.0 supports PHP 7.0+ on Windows and Linux Version 3.2 supports PHP 5.6, 5.5, and 5.4 on Windows Version 3.1 supports PHP 5.5 and 5.4 on Windows Version 3.0 supports PHP 5.4 on Windows – Balaji Sep 07 '19 at 02:58
  • https://www.microsoft.com/en-us/download/details.aspx?id=20098 you just read in system requirements,you can use 7 or 5.x too – Balaji Sep 07 '19 at 03:02
  • 1
    Your answer is using mssql, sqlsrv and pdo as interchangeable terms. They're actually three different extensions (although sqlsrv also includes a pdo driver). – Álvaro González Sep 07 '19 at 16:27