Solved

How do I convert all my mssql_ functions to sqlsrv_ after upgrading from php 5.2 to 5.4

Posted on 2014-11-14
7
213 Views
Last Modified: 2014-11-14
Hi

I have just had to upgrade my server following a hard disk failure. The window 2008 server used to run a number of PHP 5.2 applications with a MSSQL 2008 database very happily.

My new server has arrived with PHP 5.4 or 5.5, and SQL Server 2012 and none of my scripts work anymore because the mssql_connect/mssql_query etc functions are no longer supported wince PHP 5.3

Does this mean that I have to entirely re-write my applications with sqlsrv_connect/sqlsrv_query etc?

Surely this cannot be possible!

I have tried to download a copy of PHP 5.2, but before I pursue this path, can anyone suggest how I can update all my code, literally tens of thousands of lines, to work with the mssql_connect etc library.
0
Comment
Question by:rwlloyd71
  • 4
  • 3
7 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40443283
It is possible and Microsoft has mandated it.  You need the SQLSRV30 driver.

The code for the two functions, mssql and sqlsrv, are not identical and can not be simply replaced.  The thing you must change is the connection code and the names of the functions.  The SQL strings do not have to be changed.  Here is my demo code for the 'sqlsrv' driver.
<?php 
// DB configuration
$dbtable = "websitelist";  // the table we're using
/* Specify the server and connection string attributes. */
$serverName = "YOURMACHINE\SQLEXPRESS";    // Your database server
//$serverName = "10.202.46.40";    // Your database server
/* Get UID and PWD from application-specific files.  */
$uid = "dibtest";      // Your db username
$pwd = "Test4dib";      // Your db password
$connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>"TestDB");
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
		{
     echo "Unable to connect.</br>";
     die( print_r( sqlsrv_errors(), true));
		 }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>sqlsrv demo</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" summary="">
<tr valign="top">
<td>
<h2 align="center" style="margin: 1px;">sqlsrv demo - PHP/MS-SQL</h2>
<div align="center">MS SQL server using PHP 'sqlsrv' driver <?php echo $serverName; ?></div>
<table border="0" cellpadding="0" cellspacing="1" summary="local people" width="960px" bgcolor="#336699" style="font-family: Arial; font-size: 11pt;">
<tbody>
<?php
if ($conn != "") { 
// Formulate Query
$pgsize = 20;
/* Assign literal parameter values. */
$params = array( 5, 10);
sqlsrv_query( $conn,"SET ROWCOUNT $pgsize");
// --------- Get data from table ---------------
$dquery = "SELECT TOP ($pgsize) * FROM $dbtable";
$dquery .= " ORDER BY ent_num";
// TEST PRINT!!
//echo $dquery."<br>";
//if($result = sqlsrv_query( $conn, $dquery, $params, array( "Scrollable" => SQLSRV_CURSOR_KEYSET ))) {
if($result = sqlsrv_query( $conn, $dquery)) {
  //echo "Statement executed.\n";
	} 
else {
      echo "Error in statement execution.\n";
      die( print_r( sqlsrv_errors(), true));
	}
$nrows = sqlsrv_num_rows($result);

while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
echo '<tr bgcolor="#ffffff">';
echo '<td>&nbsp;'.$row[0].'</td>';
echo '<td>&nbsp;'.$row[1].'</td>';
echo '<td>&nbsp;'.$row[3].'</td>';
echo '<td>&nbsp;'.$row[4].'</td>';
echo '<td>&nbsp;'.$row[5].'</td>';
echo '<td>&nbsp;'.$row[6].'</td>';
echo '</td></tr>';
echo "\r\n";
}
}
?>
</tbody> 
</table>

</td>
</tr>
</table>

</body>
</html>

Open in new window

0
 

Author Comment

by:rwlloyd71
ID: 40443729
Thanks Dave, unfortunately, you've confirmed everything that I have been  reading on-line all day! Looks like I've got a lot of work to do!
0
 

Author Comment

by:rwlloyd71
ID: 40443738
I've requested that this question be closed as follows:

Accepted answer: 0 points for rwlloyd71's comment #a40443729

for the following reason:

Straight forward answer with useful code to a problem that was giving me grief!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40443739
You're giving your own comment as the solution.  Is that what you meant to do?
0
 

Author Comment

by:rwlloyd71
ID: 40443744
Sorry! New to this!
0
 

Author Closing Comment

by:rwlloyd71
ID: 40443748
Straight forward answer with useful code to a problem that was giving me grief!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40443914
You're welcome, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php email 2 26
php call to a non-object 3 32
Download a website to hdd 2 49
WordPress TK Title 8 24
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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