Solved

PHP - Can't connect to MSSQL

Posted on 2014-01-30
11
876 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 16

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
 
LVL 16

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

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 108

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 16

Accepted Solution

by:
Chris Harte earned 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now