Solved

Passed by reference notices in legacy php code

Posted on 2012-03-21
2
299 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
2 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to refresh a page from a sub domain in ajax ? 34 43
php subtract from date 2 26
How to build a web site 17 49
Why is my $_POST not going to results page 10 33
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
The viewer will learn how to dynamically set the form action using jQuery.

713 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