0

Sorry for the beginner question. I'm searching about an hour, but I can't understand why my $row outside from the second while doesn't function... The name variable run just the $row var doesn't function...

$i = 0;
while($i < 8)
{
    $str = "SELECT * FROM `$name[$i]`";
    $result = mysql_query($str, $connessione);
    $l = mysql_num_rows($result);

    while($l > 1)
    {
        $strs = "SELECT * FROM `$name[$i]` WHERE `Livello` = '$l'";
        $results = mysql_query($strs, $connessione);
        $row[$i][$l] = mysql_fetch_array($results);

            if I put here the echo I can view the mysql variable 
            echo $row[$i][$l]['var'];

        $l--;
    }
        if I put here echo $row[$i][$l]['var']; he send me the error " Undefined offset"
    $i++;
}

Hope you can help me...

3
  • What does "doesn't function" mean?! Commented Jul 7, 2014 at 11:00
  • 1
    what is if mysql_num_rows($result) return 1, then $l > 1 is false. Commented Jul 7, 2014 at 11:03
  • 1
    If $l is 0 to begin with, the $row variable will not be defined. Commented Jul 7, 2014 at 11:03

2 Answers 2

1

In the place where you put:

echo $row[$i][$l]['var']; 

$l value is 0 and you set $row values for $l from 1 to mysql_num_rows($result)

if you put there:

echo $row[$i][1]['var'];

it should work fine assuming mysql_num_rows($result) was more than 1 element.

Probably your code should look like this:

$i = 0;
while($i < 8)
{
    $str = "SELECT * FROM `$name[$i]`";
    $result = mysql_query($str, $connessione);
    $l = mysql_num_rows($result);

    while($l > 0) // changed 1 to 0
    {
        $strs = "SELECT * FROM `$name[$i]` WHERE `Livello` = '$l'";
        $results = mysql_query($strs, $connessione);
        $row[$i][$l] = mysql_fetch_array($results);

            if i put here the echo i can view the mysql variable 
            echo $row[$i][$l]['var'];

        $l--;
    }

    // added extra loop to display array values
    $whileIndex = 0;
    while (true) {
      if (!isset($row[$i][$whileIndex]['var']) {
         break;
      }
      echo $row[$i][$whileIndex]['var']; // should work
      ++$whileIndex;
    }


    $i++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

in the first echo i can view the code is in the second the problem... if i put your code i got the same problem
I've added possible solution to my answer
0

When you try to use echo, $i = 8 and $l = 1. These keys doesn´t exists in your array.

2 Comments

i want the variable for the $i between 0 and 7 (8 vars) and for $l between 1 and mysql num rows (different var)
You have to use echo in second loop, not behind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.