reference errors from PHP 4.4.0 update

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buvwon
Forum Newbie
Posts: 2
Joined: Sun Jun 04, 2006 11:03 am

reference errors from PHP 4.4.0 update

Post by buvwon »

**********UPDATE***********************
Ok... so I was able to fix all of the errors by deleting the ampersand except one. The one that is still all over the site is
Notice: Only variable references should be returned by reference in /home/wedweb/public_html/library/database.php on line 26
This is line 26:

Code: Select all

return mysql_fetch_assoc($result);
Here is the entire database.php file in case it helps. Also, it seems like the other lines of code are very similar to line 26 and if it is causing an error, shouldn't the others be as well?

Code: Select all

<?php
require_once 'config.php';

$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());

function &dbQuery($sql)
{
	$result = mysql_query($sql) or die("Error: ".mysql_error());
	return $result;
}

function dbAffectedRows()
{
	global $dbConn;
	
	return mysql_affected_rows($dbConn);
}

function &dbFetchArray($result, $resultType = MYSQL_NUM) {
	return mysql_fetch_array($result, $resultType);
}

function &dbFetchAssoc($result)
{
	return mysql_fetch_assoc($result);
}

function &dbFetchRow($result) 
{
	return mysql_fetch_row($result);
}

function dbFreeResult($result)
{
	return mysql_free_result($result);
}

function dbNumRows($result)
{
	return mysql_num_rows($result);
}

function dbSelect($dbName)
{
	return mysql_select_db($dbName);
}

function dbInsertId()
{
	return mysql_insert_id();
}
?>


**********ORIGINAL POST*****************
So I've researched this to the best of my ability online. As far as I can tell, what has happened is that my webhost updated PHP to 4.4.2 and it created a number of variable reference errors on my site.

Here is my site: http://www.weddingwebsites.com

The errors I am getting are like this:
Notice: Only variables should be assigned by reference in /home/wedweb/public_html/library/review-functions.php on line 210
Notice: Only variable references should be returned by reference in /home/wedweb/public_html/library/database.php on line 26
Notice: Only variables should be assigned by reference in /home/wedweb/public_html/library/review-functions.php on line 219
Notice: Only variables should be assigned by reference in /home/wedweb/public_html/library/compairesite-functions.php on line 61

The errors seem to be in only about 5 or 6 php files.

I found this after doing a search on the error:
http://codex.gallery2.org/index.php/Gal ... s_wrong.3F

I wrote my localhost and he responded with this:
Actually that is the exact resource I was looking at, we are running php 4.4.2 and using 2.6.2 of the zend optimizer. I tried to kick the optimization_level down to 14 but that didn't have any effect. And as for suppression warning that would be a problem since these warnings that are present are there in place of the actual content, disabling the warnings would probably just result in blank areas.

From what I understand the issue is that passing by reference like this is rather problematic since as soon as the function is completed all associated data is destroyed.
Here is some of the code that is being referenced by the error messages:

Code: Select all

$result =& dbQuery($sql);

Code: Select all

return mysql_fetch_assoc($result);

Code: Select all

$result1 =& dbquery($sql1);

Code: Select all

$avgrating_result =& dbquery($select_avgrating);
I also found this resource http://www.simplemachines.org/community ... rdseen#new which recommended the ampersands be removed. That would seem to apply to 3 of the 4 above, but doesn't help me with the most prevalent error (the second one).

So, I guess I'd like to know if I could just edit these code lines myself or if it's going to take siginificant rewriting of the code?

Thanks in advance for the help![/quote]
Last edited by buvwon on Sun Jun 04, 2006 11:49 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think most of those error messages are just informative -- the PHP folks telling you that something has changed. There is one line that does need to be changed (if you are returning by reference) and that is:

Code: Select all

function & myFunc() {
...
return mysql_fetch_assoc($result);
Which should be:

Code: Select all

function & myFunc() {
...
$row = mysql_fetch_assoc($result);
return $row;
That is the main change when upgrading code is to always return a variable when returning by reference.
(#10850)
buvwon
Forum Newbie
Posts: 2
Joined: Sun Jun 04, 2006 11:03 am

Post by buvwon »

Thanks so much, aborint! That cleaned it up!!!

I've added the entire database.php file above. Do I need to do that for all those functions?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

buvwon wrote:I've added the entire database.php file above. Do I need to do that for all those functions?
Only the ones that return my reference -- in your code that's also &dbFetchArray(), &dbFetchAssoc(), &dbFetchRow(). The rule now is: all functions that return by referenece must return an actual variable.

This change was a break in backward compatabiity that really annoyed a lot of the community, mainly because of how it was handled which was with little notice and no attempts to manage the effects.
(#10850)
Post Reply