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'];
....
精彩评论