Jump to content

php3 to php5 issue


funkyres

Recommended Posts

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.

Link to comment
Share on other sites

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};

Link to comment
Share on other sites

/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 :-\

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.