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 http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=20098 in compliance with there : http://www.php.net/manual/en/mssql.requirements.php

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

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.