funkyres Posted October 29, 2007 Share Posted October 29, 2007 I'm not a php guru - but I do have a little experience in the past with php4. I'm trying to help a friend get his site updated from php3 to php5. Most of the issues I've been able to figure out. This one I'm having trouble understanding what is going on which makes it hard for me to fix. The code was written by somebody else years ago. $sql = "SELECT image_id FROM images"; $img_result = mysql_query($sql); while($img_rs = mysql_fetch_object($img_result)) { if(${$img_rs->image_id} == "1") { $sql = "UPDATE images SET fid='$new_fid' WHERE image_id = '$img_rs->image_id'"; $result = mysql_query($sql); } } It works in php3 - in php5 it get stucks in the while loop with: Notice: Undefined variable: 217 in /srv/blah.php with the number continually changing each time through the loop. And it pounds the database. Is anyone here able to understand what that loop is trying to do and why it fails in php5 ? I have bought a couple php/MySQL books that are more modern than what I previously owned, but I'm kind of stuck at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/75252-php3-to-php5-issue/ Share on other sites More sharing options...
funkyres Posted October 29, 2007 Author Share Posted October 29, 2007 I tried the following just before the while loop: $img_rs = mysql_fetch_object($img_result); $foo = ${$img_rs->image_id}; print "$foo"; die ('that was foo'); The script exits with the warning on the foo line before it gets to the print statement. So I think the issue (or at least one of them) is the ${$img_rs->image_id}; Quote Link to comment https://forums.phpfreaks.com/topic/75252-php3-to-php5-issue/#findComment-380611 Share on other sites More sharing options...
Dragen Posted October 29, 2007 Share Posted October 29, 2007 /I've never seen a variable like this before: ${$img_rs->image_id} But I've never dealt with php below 4. It looks like a class variable to me. Try changing it to this: $$img_rs->image_id or if that still fails give this a go: $img_rs->image_id I'm not sure if you're wanting the extra $ there or not for this :-\ Quote Link to comment https://forums.phpfreaks.com/topic/75252-php3-to-php5-issue/#findComment-380613 Share on other sites More sharing options...
kenrbnsn Posted October 29, 2007 Share Posted October 29, 2007 This construct <?php ${$img_rs->image_id} ?> is a variable variable. It looks like the code is attempting to see if the variable name the value of $img_rs->image_id is equal to 1. Since this value keeps incrementing this if statement will only be true once. But the since the ID is numeric and you can't have a variable that starts with a number, this statement is invalid. If you change the code to <?php while($img_rs = mysql_fetch_object($img_result)) { if($img_rs->image_id == "1") { $sql = "UPDATE images SET fid='$new_fid' WHERE image_id = '$img_rs->image_id'"; $result = mysql_query($sql); } }?> It will be syntacticly correct, but probably not logically correct. Ken Quote Link to comment https://forums.phpfreaks.com/topic/75252-php3-to-php5-issue/#findComment-380615 Share on other sites More sharing options...
funkyres Posted October 29, 2007 Author Share Posted October 29, 2007 OK - I think I may have some idea. <?php while($img_rs = mysql_fetch_object($img_result)) { //stuff } ?> It looks like that loop was intended to handle multiple results. I think a for each loop would have been better, I'll have to play with it some more. I changed it to just <?php $img_rs = mysql_fetch_object($img_result); ?> and (with fix below) it seems to do what it is suppose to do when there is a single thing to do. When the web interface asks it to do more than one, it only does the first one - but that it does one and one correctly indicated to me I just need to put it in a for each loop and I should be golden. <?php if(${$img_rs->image_id} == "1") { $sql = "UPDATE images SET fid='$new_fid' WHERE image_id = '$img_rs->image_id'"; $result = mysql_query($sql); } ?> I think the if statement was a php3 attempt at something like IsSet - and was a boolean test to make sure the variable was set. I don't know that for sure, but I changed it to if (IsSet($img_rs->image_id)) { and it seems to do what it is suppose to do. I'm not positive that's what the original coder intended - there are plenty of other places in the code where he should have tested for things before having the script act and he didn't, but I think that's what he was trying to do here. Quote Link to comment https://forums.phpfreaks.com/topic/75252-php3-to-php5-issue/#findComment-380715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.