Solved

PHP - Can't connect to MSSQL

Posted on 2014-01-30
11
901 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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