首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php中使用函数返回多行

问如何在php中使用函数返回多行
EN

Stack Overflow用户
提问于 2013-01-04 12:22:22
回答 3查看 1K关注 0票数 0

请看下面的代码:

代码语言:javascript
复制
<?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%正常工作。问题出在我的函数上。请告诉我哪里出了问题,以及如何修复它。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-04 12:26:03

在while语句中将$row_data更改为$row

代码语言:javascript
复制
while($row = mysql_fetch_array($row_data))

因为在我看来,while中的代码

代码语言:javascript
复制
$rValue = $row['pic'];
$lValue = $row['mn_title'];

您以$row格式获取数据,但in while语句为$row_data

问题不在while循环中当执行到达return语句时,执行指针将退出函数(当然是在while语句中)

但是为了使您的代码更简洁,因为我看到您希望返回的代码只有一行,所以在您的while上拉出return语句

代码语言:javascript
复制
$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行返回,您可以创建一个变量,将所有返回存储在单个字符串中

代码语言:javascript
复制
   $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

票数 2
EN

Stack Overflow用户

发布于 2013-01-04 12:25:40

代码语言:javascript
复制
    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;
}

编辑:已更新的变量名称

票数 1
EN

Stack Overflow用户

发布于 2013-01-04 12:29:17

它没有给出完整的结果集,因为你是从循环中“返回”的。尝试下面的方法,它应该会有帮助。

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14151049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档