Solved

PHP - Can't connect to MSSQL

Posted on 2014-01-30
11
892 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 109

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 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