-1

Something is wrong with my syntax. I am trying to use a php variable as the column name of the row I am currently reading. below is a snippet of my code. The first echo of folder properly displays for word "organizations" (the column name I want, and the last echo properly display the contents of the $row_ridoh['organization'] which is 1, but the echo in the middle with the variable coded does not work. Please advise.

while($row_ridoh = mysql_fetch_array($ridoh)) {
  echo('folder: ' . $folder);
  echo('heres the real PROBLEM: ' . $row_ridoh[${$folder}]);
  echo('</br>row organizations: ' . $row_ridoh['organizations']);
  if ($row_ridoh["{$folder}"] == '1') {
    echo('keyword in if: ' . $folder);
  }
}

I made the suggested changes to code:

     while($row_ridoh = mysql_fetch_array($ridoh)){
      echo('folder: ' . $folder);
      echo('here the real PROBLEM: ' .  $row_ridoh[$folder]);
        echo('</br>row organizations: ' . $row_ridoh['organizations']);
        if ($row_ridoh[$folder] == '1')
        {  echo('keyword in if: ' . $folder); }
      }

The echo for 'REAL PROBLEM" still not displaying. Here is my output when executed:

folder: organizations

Notice: Undefined index: organizations in /export/webs/inside.health/includes/sql/checkuser.php on line 30

here the real PROBLEM:

row organizations: 1

Again - thanks for the help. I updated the code to include the var_dump. It is listed below:

array(26) { [0]=> string(1) "1" ["id"]=> string(1) "1" [1]=> string(5) "admin" ["username"]=> string(5) "admin" [2]=> string(8) "r1health" ["password"]=> string(8) "r1health" [3]=> string(19) "2017-06-29 10:48:34" ["modified"]=> string(19) "2017-06-29 10:48:34" [4]=> string(1) "1" ["accomplishments"]=> string(1) "1" [5]=> string(1) "1" ["actions"]=> string(1) "1" [6]=> string(1) "1" ["complaints"]=> string(1) "1" [7]=> string(1) "1" ["diseases"]=> string(1) "1" [8]=> string(1) "1" ["entity"]=> string(1) "1" [9]=> string(1) "1" ["milestones"]=> string(1) "1" [10]=> string(1) "1" ["organizations"]=> string(1) "1" [11]=> string(1) "1" ["publications"]=> string(1) "1" [12]=> string(1) "1" ["specimens"]=> string(1) "1" }

It does have the column name correctly labeled as 'organizations'. The $folder variable is set in code above. As displayed in the echo output "folder: organizations".

Looking forward to other suggestions to try. Do you think it has anything to do with my version of php 5.1.6?

  • You don't need to quote it, just use `$row_ridoh[$folder]`, if that variable has a string or number value, it will work – Ajaypayne Jul 05 '17 at 17:33
  • Thanks for the suggestion. I tried that also, but still didnt work. Here is the output with your suggestion: folder: organizations Notice: Undefined index: organizations in /export/webs/inside.health/includes/sql/checkuser.php on line 30 here the real PROBLEM: row organizations: 1 – ridohealth Jul 05 '17 at 17:36
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 05 '17 at 17:40
  • 1
    Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799), and `mysql_*` functions have been officially removed in PHP 7. Instead you should learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) and use either `PDO` or `mysqli_*`. If you can't decide, [this article will help to choose your best option](http://php.net/manual/en/mysqlinfo.api.choosing.php). – GrumpyCrouton Jul 05 '17 at 17:40
  • Also, why are you doing this? `$row_ridoh[${$folder}]`? I believe that is trying to access a variable named whatever `$folder` stores. For example, if `$folder` was `banana` that would be accessing a variable called `$banana`. There are no syntax issues in your code except for the deprecated `mysql_*` function use, all of the other syntax works but maybe not for what you are trying to do. – GrumpyCrouton Jul 05 '17 at 17:48
  • Add this in the first line of the while loop: var_dump($row_ridoh); Update your question with the var_dump of the structure. – gview Jul 05 '17 at 17:59
  • Also where is the value of $folder set? Where is that code? – gview Jul 05 '17 at 18:01
  • Thanks. I updated my question as you requested. – ridohealth Jul 06 '17 at 13:43

1 Answers1

1

This syntax, ${$folder}, tries access a variable that has the same name as the string stored in $folder. From what I gather, you just want the string stored in $folder. So, change $row_ridoh[${$folder}] to $row_ridoh[$folder].

EDIT:

I just noticed this as well: if ($row_ridoh["{$folder}"] == '1') {. This is different from what you're printing out and is also wrong. This should be changed from $row_ridoh["{$folder}"] to $row_ridoh[$folder].

sorayadragon
  • 1,087
  • 7
  • 21
  • Thanks for the reply. I did try your suggestion: still not working. Here is the snippet of code with change: while($row_ridoh = mysql_fetch_array($ridoh)){ echo('folder: ' . $folder); echo('here the real PROBLEM: ' . $row_ridoh[$folder]); echo('row organizations: ' . $row_ridoh['organizations']); if ($row_ridoh[$folder] == '1') { echo('keyword in if: ' . $folder); } } – ridohealth Jul 05 '17 at 17:45
  • Please see my edit to my original post with new code and executed output. THANKS. – ridohealth Jul 05 '17 at 18:40
  • Odd that the error was printed before your echo...check the spelling of 'organizations' in the `$folder` variable, perhaps it's not correct. If you can access the element via `$ridoh['organizations']`, you should be able to access it via `$ridoh[$folder]` assuming that `$folder` is a string with the value 'organizations`. – sorayadragon Jul 05 '17 at 18:46
  • That's what I thought... it doesn't seem to be working for me.? I did check the spelling of the variable and the column name.. see var_dump in my edited question. Any other ideas? – ridohealth Jul 06 '17 at 16:28