0

Look at the following code :

<?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>";
    }
}

this function is not returning anything! I am thinking it is because that the return statement is inside the while loop. I tried many things hoping it will return the 4 results but nothing happened. the SQL query works 100%. the problem is with my function. please let me know what is wrong and how to fix it.

4

3 に答える 3

2

ステートメントの$row_dataをに変更$rowしますwhile

while($row = mysql_fetch_array($row_data))

私が内部のコードを見るとwhile

$rValue = $row['pic'];
$lValue = $row['mn_title'];

あなたはあなたのデータを次のように取得します$rowが、あなたのinwhileステートメントは$row_data

実行がステートメントに到達したときに問題がwhileループにないreturn場合、実行ポインターは関数を終了します(もちろんwhileステートメント内で)

しかし、私があなたのコードをよりクリーンにするために、あなたは見返りに1行だけを期待している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行のリターンを期待していると言っているように、すべてのリターンを1つの文字列に格納する変数を作成できます

   $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

于 2013-01-04T04:26:02.823 に答える
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;
}

編集:変数名を更新

于 2013-01-04T04:25:39.590 に答える
1

Its not giving full result set because you are 'returning' from within the loop. try the following it should help.

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;
}
于 2013-01-04T04:29:17.253 に答える