开发者

Having trouble parsing a MySQL result in php

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 "<i开发者_如何学Pythonmg 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.


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


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'];
  ....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜