Go Premium for a chance to win a PS4. Enter to Win

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
?
325 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 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
Technology Partners: 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

886 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