请查看以下代码:
<?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>";
}
}
这个功能没有返回任何东西!我认为这是因为return语句在while循环中。我尝试了很多东西,希望它会返回4个结果,但什么都没发生。 SQL查询100%工作。问题在于我的功能。请告诉我有什么问题以及如何解决。
答案 0 :(得分:2)
将$row_data
声明中的$row
更改为while
while($row = mysql_fetch_array($row_data))
因为我看到了while
$rValue = $row['pic'];
$lValue = $row['mn_title'];
您的数据为$row
但您的while
声明为$row_data
问题不在while
循环上,因为执行到达return
语句,执行指针将退出函数(当然在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
<强>参考强>: http://php.net/manual/en/function.mysql-fetch-array.php
答案 1 :(得分:1)
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;
}
编辑:更新了变量名称
答案 2 :(得分:1)
它没有给出完整的结果集,因为你在循环中'返回'。尝试以下它应该有所帮助。
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;
}