请看下面的代码:
<?php
function getmangainfo($teamname)
{
$rValue = "";
$lValue = "";
$query = ("SELECT pic, mn_title FROM table Where mn_team LIKE '%" . $teamname . "%' Limit 0,4 ");
$row_data = mysql_query($query);
while ($row_data = mysql_fetch_array($row_data)) {
$rValue = $row['pic'];
$lValue = $row['mn_title'];
return "<a class='linksrepeated' href='" . $ABSPATH . "/" . $lValue . "/'> <img src='" . $rValue . "'/></a>";
}
}此函数不会返回任何内容!我认为这是因为返回语句在while循环中。我尝试了很多方法,希望它能返回4个结果,但什么也没发生。SQL查询可以100%正常工作。问题出在我的函数上。请告诉我哪里出了问题,以及如何修复它。
发布于 2013-01-04 12:26:03
在while语句中将$row_data更改为$row
while($row = mysql_fetch_array($row_data))因为在我看来,while中的代码
$rValue = $row['pic'];
$lValue = $row['mn_title'];您以$row格式获取数据,但in while语句为$row_data
问题不在while循环中当执行到达return语句时,执行指针将退出函数(当然是在while语句中)
但是为了使您的代码更简洁,因为我看到您希望返回的代码只有一行,所以在您的while上拉出return语句
$rValue = "";
$lValue = "";
while ($row_data = mysql_fetch_array($row_data)) {
$rValue = $row['pic'];
$lValue = $row['mn_title'];
break; //just to make sure for one row return
}
return "<a class='linksrepeated' href='" . $ABSPATH . "/" . $lValue . "/'> <img src='" . $rValue . "'/></a>";但是,正如其他人所说的那样,您需要4行返回,您可以创建一个变量,将所有返回存储在单个字符串中
$rValue = "";
$lValue = "";
$links = "";
while ($row_data = mysql_fetch_array($row_data)) {
$rValue = $row['pic'];
$lValue = $row['mn_title'];
$links .="<a class='linksrepeated' href='" . $ABSPATH . "/" . $lValue . "/'> <img src='" . $rValue . "'/></a>";
}
return $links发布于 2013-01-04 12:25:40
function getmangainfo($teamname){
$rValue = "";
$lValue = "";
$query = ("SELECT pic, mn_title FROM table Where mn_team LIKE '%".$teamname."%' Limit 0,4 ");
$row_data = mysql_query($query);
$output=array();
while($row = mysql_fetch_array($row_data))
{
$rValue = $row['pic'];
$lValue = $row['mn_title'];
$output[]="<a class='linksrepeated' href='".$ABSPATH."/".$lValue."/'> <img src='".$rValue."'/></a>";
}
return $output;
}编辑:已更新的变量名称
发布于 2013-01-04 12:29:17
它没有给出完整的结果集,因为你是从循环中“返回”的。尝试下面的方法,它应该会有帮助。
function getmangainfo($teamname){
$rValue = "";
$lValue = "";
$query = ("SELECT pic, mn_title FROM table Where mn_team LIKE '%".$teamname."%' Limit 0,4 ");
$row_data = mysql_query($query);
$return = '';
while($row_data = mysql_fetch_array($row_data))
{
$rValue = $row['pic'];
$lValue = $row['mn_title'];
$return .= "<a class='linksrepeated' href='".$ABSPATH."/".$lValue."/'> <img src='".$rValue."'/></a>";
}
return $return;
}https://stackoverflow.com/questions/14151049
复制相似问题