Hi There;
I have a problem during connection to a MS-SQL server. I copy the code there: http://www.webcheatsheet.com/PHP/connect_mssql_database.php

<?php
$myServer = "localhost";
$myUser = "Murat";
$myDB = "examples"; 

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

//Point 2

//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);
?>

When I write echo "hello"; to point 1, it can be seen, however, when I write it to point 2, nothing has appeared in my browser.I presume that I face a connection problem but I couldn't overcome. I've installed necessary database driver named "SqlSrv" to c:\program files\php\ext from in compliance with there :

Any help is greatly appreciated. Here is my technical infrastructure:

PHP Version: PHP Version 5.3.8
Internet Server: IIS 5.1
OS: Windows XP Professional Service Pack 3

Dani AI

Generated

Quick summary: solved the immediate problem by using ODBC, which is a perfectly valid fallback for Windows + IIS. If you want the Microsoft PHP driver (sqlsrv / pdo_sqlsrv) to work instead, the usual causes are mismatched binaries (PHP build vs driver: x86 vs x64, thread-safe vs non-thread-safe), a missing ODBC/Native Client or Visual C++ runtime, or the driver not being loaded in php.ini. The Microsoft msphpsql project and release notes explain the different DLL names and build variants to match your PHP build. msphpsql (GitHub). (github.com)

Checklist to diagnose the sqlsrv/pdo_sqlsrv path (quick, ordered):

  • Run a tiny phpinfo() page and confirm the driver appears and note the PHP architecture (x86/x64) and thread-safety.
  • Ensure the exact driver DLL you downloaded sits in PHP's ext folder and that php.ini has the correct extension line (use the filename that matches your PHP version and TS/NTS).
  • Install the Microsoft ODBC/Native Client required by that driver version, then restart IIS. The official loading instructions show this flow. Loading the drivers. (documentation.help)

Server-side connection quirks to check (common culprits):

  • Named instance: either run SQL Server Browser or connect with tcp:host\Instance or tcp:host,port to force TCP and avoid name-resolution issues.
  • Make sure TCP/IP is enabled for that instance and the firewall allows the port (default 1433 for default instances). Microsoft’s troubleshooting doc explains these network/instance behaviors. (learn.microsoft.com)

How to get useful errors and a modern API suggestion:

  • For sqlsrv, call sqlsrv_errors() when connect/query returns false to get detailed messages. For PDO, use the PDO_SQLSRV driver and set PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION so exceptions show why the connect failed. Example patterns and the sqlsrv error API are documented on php.net and Microsoft’s PDO docs; also note the old PHP mssql extension was removed in PHP 7, so prefer sqlsrv/pdo_sqlsrv or ODBC for future compatibility. sqlsrv_errors() docs, PDO_SQLSRV docs / driver notes, PHP removed extensions. (php.net)

If sqlsrv still refuses to appear in phpinfo(): capture IIS/PHP error logs, double-check the exact DLL name vs your PHP build, and confirm the correct Visual C++ redist/ODBC driver is installed — those are the usual blockers. This complements ’s pointer to the Microsoft driver and explains why ODBC sometimes ends up being the quickest workaround.

Recommended Answers

All 5 Replies

I have found that code and run it, but nothing different happened. By the way, my browser is IE 8.Here is my new code:

$dbserver = "REHAB-EB07315EE\BWDATOOLSET";

    $dbname = "osman";
    
    die("go shawdy it's your database");
    $connection = dbo_logon($dbserver,$dbname);  
   
    function dbo_logon($dbserver,$dbname) {  
    
        $connectionOptions = array("Database"=>$dbname);  
        $dbh=sqlsrv_connect($dbserver, $connectionOptions);  
        
        if(!$dbh){  
        
            die("Error in Database connection");  
            return false;   
            
        }else
        {
            die("Well done.");
            return $dbh;   
        }   
     
     }

It only says, go shawdy it's your database. It neither says Error in Database connection nor Well done. when I expect seeing either.

You have not passed password into mssql_connect()
There should be 3 parameters in mssql_connect()'
host,username & password

Try this :
<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";

//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");
or die("Couldn't connect to SQL Server on $myServer");

die() ends your script execution. It is the same thing as exit(). So when your script hits the line die("go shawdy it's your database"); it exits and never executes the rest fo the script. Replace the die call with echo 'go shawdy it's your database'; and do the same for the other two in your function.

hi there again;
I solved the problem with the code below:

$virtual_dsn = 'DRIVER={SQL Server};SERVER=REHAB-EB07315EE\BWDATOOLSET;DATABASE=dbname';
$connection = odbc_connect($virtual_dsn,'username','password') or 
        die('ODBC Error:: '.odbc_error().' :: '.odbc_errormsg().' :: '.$virtual_dsn);

odbc_exec($connection,'dbname');

$result = odbc_exec($connection, "SELECT * FROM tablename WHERE name = 'osman'");

I couldn't manage to connect with sqlsrv_connect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.