Solved

Passed by reference notices in legacy php code

Posted on 2012-03-21
2
287 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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

18 Experts available now in Live!

Get 1:1 Help Now