views:

99

answers:

2

I'm trying to pull some results from a database but I guess I'm kind of brain dead today.

$castquery = "SELECT * FROM cast " .
    "WHERE player_id = '" . $userid ."' ";
$castresult = mysql_query($castquery) or die(mysql_error());
$castrow = mysql_fetch_array($castresult);

...

foreach($castrow['cast_id'] as $caster) 
{
    echo "<p>";
    if ($caster['avatar_url']!='') echo "<img src=\"".$caster['avatar_url']."\" alt=\"".$caster['name']."\">";
    echo "<a href=\"?edit=".$caster['cast_id']."\">".$caster['name']."</a></p>";
}

Surely I'm overlooking something obvious here.

+5  A: 

Not sure what your db structure is, but $castrow['cast_id'] is a single field, not an array. What you probably mean is:

while ( $castrow = mysql_fetch_array($castresult) ) {
    // use $castrow array here
}

instead of your foreach

kemp
`castrow` should technically be called `castrows`, as it's the set of rows rather than one row.
adam
Well, technically, every time you call `mysql_fetch_array()` you get *one* row, not many.
kemp
Thanks, I knew it was something simple and dumb.
aslum
+1  A: 

First of all, your code will only fetch one row as it is now.

$castrow = mysql_fetch_array($castresult);

foreach($castrow['cast_id'] as $caster) 
{

Should be

while ($castrow = mysql_fetch_array($castresult)) {

  $caster = $castrow['cast_id'];
  ....
code_burgar