0

How can I echo out value after the while loop. If I do the echo in below code its says Undefined index.

$sql_cast = "SELECT *
            FROM
              title_cast
              INNER JOIN title ON (title_cast.id_title = title.id)
              INNER JOIN `cast` ON (title_cast.id_cast = `cast`.id)
            WHERE
              title_cast.id_title = '1'";
$result_cast = mysql_query($sql_cast) or die('log error with' .mysql_error());
$cast = array();
while ($row = mysql_fetch_assoc($result_cast)) {
                $id = $row['id'];
                $name = $row['name'];
                $img = $row['photo_localurl'];
                $poster = str_replace("./", "lib/", $img);

                $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
                //$cast[] = $row;
                }
                //var_dump($cast);
                echo $cast['id'] . " " . $cast['name'] . " " . $cast['poster']."<br />";
1
  • Try var_dump($row) first in the while loop. Also, I would expect while( ($row = mysql_fetch_assoc($result_cast)) !== FALSE ) { or similar Commented Feb 3, 2012 at 8:57

6 Answers 6

2

Within the while loop, you set the cast array content using $cast[] syntax. This will create a numerical index, starting at 0, then 1 and so on, so you're creating an array that looks like this:

$cast = array(
    0 => array('id' => $id, 'name' => $name, 'img' => $poster),
    1 => array('id' => $id, 'name' => $name, 'img' => $poster)
);

You need to include the numerical key of the array that you want to echo. For example, if you want to echo the first row:

echo $cast[0]['id']; // Echo the id of the first result

If you want to echo ALL of the rows, use foreach:

foreach($cast as $row) {
    echo $row['id'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you should do:

$cast = array('id' => $id, 'name' => $name, 'img' => $poster);
                //$cast[] = $row;
                }
                //var_dump($cast);
echo $cast['id'] . " " . $cast['name'] . " " . $cast['poster']."<br />";

because if you use $cast[], it will append the new array to your array..

Comments

0

That's because you are pushing a new array in $cast at each index..

So you should echo like this..

$i = 0;
while ($row = mysql_fetch_assoc($result_cast)) {
      $id = $row['id'];
      $name = $row['name'];
      $img = $row['photo_localurl'];
      $poster = str_replace("./", "lib/", $img);

      $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
      //$cast[] = $row;

      //var_dump($cast);
      echo $cast[$i]['id'] . " " . $cast[$i]['name'] . " " . $cast[$i]['poster']."<br />";
      $i++;
}

Comments

0

Try this:

<?php
while ($row = mysql_fetch_assoc($result_cast)) {
      $id = $row['id'];
      $name = $row['name'];
      $img = $row['photo_localurl'];
      $poster = str_replace("./", "lib/", $img);

      $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
}

foreach($cast as $rows) {
    echo $rows['id']." ".$rows['name']." ".$rows['img']."<br />";
}
?>

Comments

0
print_r($cast[0]); or you can use $cast[0]['id'] ;

Comments

0

There are two problems with your code, First is that $cast[] is a two-dimensional array so it contains arrays at each of its index, so you must use

$cast[$i]['id'] 

Where i will be the counter variable that will iterate through all the indexes. Secondly change

$cast['poster'] 

to

$cast['img']

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.