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
256 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 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
Industry Leaders: 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!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

615 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