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.

hafichuk
  • 10,351
  • 10
  • 38
  • 53
syrkull
  • 2,295
  • 4
  • 35
  • 68
  • do - var_dump($row_data) - check what it is displaying – Joddy Jan 04 '13 at 04:25
  • Why aren't you echoing the data out? Return will not work with multiple iterations (I don't believe) – Dylan Cross Jan 04 '13 at 04:25
  • 2
    the loop will always be executed once because of the `return` statement inside it. – John Woo Jan 04 '13 at 04:25
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/a/14110189/1723893). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – NullPoiиteя Jan 04 '13 at 04:39

3 Answers3

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

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • best answer of them all. you are the chosen one :P – syrkull Jan 04 '13 at 04:34
  • you're the chosen one also! enjoy coding =) – Netorica Jan 04 '13 at 04:37
  • @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 – Hanky Panky Jan 04 '13 at 05:04
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

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
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;
}