Search Webmaster Help and Solution

[RESOLVED] Using a variable in a $row-> call from sql is breaking the code

Ive never had to use a variable in this position, but I am obviously misunderstanding something, as this is the code...

PHP Code: $row = mysql_fetch_object( mysql_query(SELECT * FROM book_bundle WHERE bundle_id=$bundleID) );
This selects a single row from my database.

PHP Code: if ( !empty($row) ) {

  if         (!empty($row->book_name_4)) {$field_count=4;}
  elseif     (!empty($row->book_name_3)) {$field_count=3;}
  elseif     (!empty($row->book_name_2)) {$field_count=2;}
  elseif     (!empty($row->book_name_1)) {$field_count=1;}
  else {}
  
  for ($i=1; $i<=$field_count; $i++){
      echo
        <img src=/images/thumbs/$row->book_image_$i.png />
        $row->book_name_$i
        <br />
  ;
  }

}
The issue is in the FOR statement. Before this was a FOR statement, I just repeated the same code 4 times, and inside the difference was that instead of $row->book_image_$i and $row->book_name_$i, the $is were 1 in the first block of code, 2 the second time, and so on. It was manually coded in.

I tried to shrink the code using the FOR statement, but I cant seem to pass a variable inside the $row-> call, without breaking it. This code never looks up the $row, it outputs just 1 the first time, 2 the second time. It disregards the $row-> and disregards all text before the $i as well. I dont understand this behavior.

On the other hand, if I change $i to 1, then it works as expected, giving me the book name and picture from the database, only it repeats the same one 4 times as expected from the FOR statement. I just cant figure out how to turn that number into a variable and have the statement work.

Also, while testing, I changed "$row->book_name_$i" to "$row->$i". This broke all code from this point forward when displayed.

Im sure theres a simple reason why variables dont work like this, or how to properly do it, but I dont know the terminology for these. Ive been searching google and php.net/manual, but without knowing what to call them, Ive had no luck discovering the answer. Thank you for any help. Ive never had to use a variable in this position, but I am obviously misunderstanding something, as this is the code...

PHP Code: $row = mysql_fetch_object( mysql_query(SELECT * FROM book_bundle WHERE bundle_id=$bundleID) );
This selects a single row from my database.

PHP Code: if ( !empty($row) ) {

  if         (!empty($row->book_name_4)) {$field_count=4;}
  elseif     (!empty($row->book_name_3)) {$field_count=3;}
  elseif     (!empty($row->book_name_2)) {$field_count=2;}
  elseif     (!empty($row->book_name_1)) {$field_count=1;}
  else {}
  
  for ($i=1; $i<=$field_count; $i++){
      echo
        <img src=/images/thumbs/$row->book_image_$i.png />
        $row->book_name_$i
        <br />
  ;
  }

}
The issue is in the FOR statement. Before this was a FOR statement, I just repeated the same code 4 times, and inside the difference was that instead of $row->book_image_$i and $row->book_name_$i, the $is were 1 in the first block of code, 2 the second time, and so on. It was manually coded in.

I tried to shrink the code using the FOR statement, but I cant seem to pass a variable inside the $row-> call, without breaking it. This code never looks up the $row, it outputs just 1 the first time, 2 the second time. It disregards the $row-> and disregards all text before the $i as well. I dont understand this behavior.

On the other hand, if I change $i to 1, then it works as expected, giving me the book name and picture from the database, only it repeats the same one 4 times as expected from the FOR statement. I just cant figure out how to turn that number into a variable and have the statement work.

Also, while testing, I changed "$row->book_name_$i" to "$row->$i". This broke all code from this point forward when displayed.

Im sure theres a simple reason why variables dont work like this, or how to properly do it, but I dont know the terminology for these. Ive been searching google and php.net/manual, but without knowing what to call them, Ive had no luck discovering the answer. Thank you for any help.

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).