Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
278 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
[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
  • 4
  • 3
7 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 84

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 84

Expert Comment

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

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

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