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
207 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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

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

Author Comment

by:rwlloyd71
Comment Utility
Sorry! New to this!
0
 

Author Closing Comment

by:rwlloyd71
Comment Utility
Straight forward answer with useful code to a problem that was giving me grief!
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 29
echo button 13 45
PHP code to retrieve NDFD weather forecasts. 6 34
php variable basic question 12 27
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 is …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now