Solved

PHP - Can't connect to MSSQL

Posted on 2014-01-30
11
910 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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 110

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 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