?
Solved

PHP - Can't connect to MSSQL

Posted on 2014-01-30
11
Medium Priority
?
979 Views
Last Modified: 2014-01-30
I am trying to get PHP to connect to MSSQL - I have the drivers installed - the php.info shows "PDO drivers  mysql, sqlite, sqlsrv" under PDO.  I have this script and I can't even get a error message that I can't connect or that it couldn't open the db.  Here is the script I am trying

<?php
$myServer = "IT_671_764\SQLEXPRESS";
$myUser = "User";
$myPass = "password";
$myDB = "TestDB";

//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, FundNumber, FundName ";
$query .= "FROM DataMatch ";
$query .= "WHERE FundName='Tax Free'";

//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["FundNumber"] . $row["FundName"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?> 

Open in new window

0
Comment
Question by:JohnMac328
  • 6
  • 4
11 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 39820951
Put this in the first line to throw a visible error

ini_set ('display_errors', 'on');

When you run phpinfo search for mssql, is should have a whole paragraph to itself.
0
 

Author Comment

by:JohnMac328
ID: 39820966
It gives me this

Fatal error: Call to undefined function mssql_connect() in C:\inetpub\wwwroot\DBTEST.php on line 17

Line 17 is this

$dbhandle = mssql_connect($myServer, $myUser, $myPass)
 Here is the paragraph on phpinfo

pdo_sqlsrv.client_buffer_max_kb_size 10240 10240
pdo_sqlsrv.log_severity 0 0
0
 

Author Comment

by:JohnMac328
ID: 39820977
So mabey this?

//create an instance of the  ADO connection object
$conn = new COM ("ADODB.Connection")
  or die("Cannot start ADO");

//define connection string, specify database driver
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
  $conn->open($connStr); //Open the connection to the database
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:Chris Harte
ID: 39821024
Since your phpinfo says you are using slqsrv, have you tried the function sqlsrv_connect()?

http://uk3.php.net/manual/en/function.sqlsrv-connect.php
0
 

Author Comment

by:JohnMac328
ID: 39821036
I changed it and it give this error

Catchable fatal error: Argument 2 passed to sqlsrv_connect() must be an array, string given in C:\inetpub\wwwroot\DBTEST.php on line 17
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 39821069
This means the database is  using the sqlsrv library of database function and not the mssql one.

http://uk3.php.net/manual/en/book.sqlsrv.php

The connect has to use an associative array. Try this

$coninfo = array("Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);

$dbhandle = sqlsrv_connect ($myServer, $coninfo)
  or die("Couldn't connect to SQL Server on $myServer");

Open in new window

0
 

Author Comment

by:JohnMac328
ID: 39821094
I see - is there another sql dll that goes into the ext folder that will allow me to use the mssql functions?
0
 

Author Comment

by:JohnMac328
ID: 39821297
Let me back up a sec - the reason I want to use MSSQL is because of the SQL Management Studio - I want to display mulitple tables in the query window - drag key fields to other tables etc and then run the query and see the results in the same window as is done with SQL Management Studio.  Do you know of any program that will do that with MySQL?  Is the MSSQL and MySQL syntax the same?  Meaning I could copy it from SQL and paste it into the MySQL and get the same results?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39821348
SQL is SQL, and MsSQL and MySQL are "mostly" the same, but there are enough differences that you can't really cut and paste.  Example: MySQL uses the LIMIT clause, whereas MsSQL uses the TOP clause.

This article uses MySQL as the underlying data base for PDO, so it may not be 100% applicable, but it shows the proper way to connect and shows how to get PDO to give you the fullest possible error reporting.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 2000 total points
ID: 39821462
This is the how to on mssql, you will need the client tool from MS for that.
http://uk3.php.net/manual/en/book.mssql.php

Workbench is the gui tool for mysql, takes a while to get into but is very powerful
http://dev.mysql.com/downloads/tools/workbench/

My tool of choice is phpmyadmin because I find it easier to use than the workbench
http://www.phpmyadmin.net/home_page/index.php
0
 

Author Closing Comment

by:JohnMac328
ID: 39821497
That should do it - Thanks!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question