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 Answers 3

2

change the $row_data to $row in your while statement

while($row = mysql_fetch_array($row_data))

because as I see the codes inside the while

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

you get your data as $row but your in while statement is $row_data

the problem is not on the while loop as the execution reaches the return statement, the execution pointer will exit the function(of course in the while statement)

but for me to make your code cleaner as i see you expect only one row in return pull out the return statement on your while

$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>";

but as the others says that you expect 4 row returns you can create a variable that would store all the returns in a single string

   $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

reference: http://php.net/manual/en/function.mysql-fetch-array.php

Sign up to request clarification or add additional context in comments.

3 Comments

best answer of them all. you are the chosen one :P
you're the chosen one also! enjoy coding =)
@Mahan I think that "break" doesn't have to be there according to her question. In question it was said "I tried many things hoping it will return the 4 results but nothing happened. " So that appears like 4 results are expected and not 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;
}

Edit: Updated variable name

2 Comments

I get "Array" as a result no data at all!
@shnisaka yes, that array contains your data. run print_r() on it rather than echo
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.