?
Solved

Passed by reference notices in legacy php code

Posted on 2012-03-21
2
Medium Priority
?
314 Views
Last Modified: 2012-04-02
I have inherited some legacy code that is throwing some php notices which I would like to fix.  Here is the current notice:
PHP Notice:  Only variable references should be returned by reference in \wwwroot\classes\model\DatabasePeer.php on line 40

Open in new window


and here is the code in DatabasePeer.php around line 40 (line 40 is line 2 here):
public static function &connect($host,$user,$pass) {        			
        		return @mssql_connect($host, $user, $pass);
        		return 1;
        }

Open in new window


This function is called with the following code:
$conn = DatabasePeer::connect($CONFIG_DATABASE_HOST, $CONFIG_DATABASE_USERNAME, $CONFIG_DATABASE_PASSWORD);
try{
	$sqldb = DatabasePeer::select_db($CONFIG_DATABASE_NAME, $conn);
}catch(Exception $e){
	die("Couldn't open database $CONFIG_DATABASE_NAME".$e);
	exit();
}

Open in new window


Any thoughts how I can change this code to prevent the notice?
0
Comment
Question by:wardjame
[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
2 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 37750052
If the code is working correctly, you can just suppress the Notice with this error reporting:

error_reporting(E_ALL ^ E_NOTICE);

Notice suppression is the standard "default" case for most PHP installations.
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 total points
ID: 37751121
That doesn't look right to me with two return statements.  It should be returning the 'resource' which is normally assigned to a variable.  Maybe this?

public static function &connect($host,$user,$pass) {        			
        		$resource = @mssql_connect($host, $user, $pass);
        		return $resource;
        }

Open in new window


http://us3.php.net/manual/en/function.mssql-connect.php

Also note that if you upgrade to PHP 5.3 or greater, the 'php_mssql' functions are obselete, will not work, and are replaced by the Microsoft PHP SQL driver as noted here: http://us3.php.net/manual/en/intro.mssql.php   The current version is here http://www.microsoft.com/download/en/details.aspx?id=20098 and will not work on anything older than Windows Vista.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

764 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