user2501237
user2501237

Reputation: 9

All rows returned in phpMyAdmin but only 1 in PHP

When I run this query in phpMyAdmin it loads all the rows. But when I run it through PHP SQL, I only get the first row. I'm not sure what I'm doing wrong here and I'm really getting stressed out.

Here's my code:

    function getRows($user)
{
    $q = mysql_query("SELECT day(closed) AS day, COUNT( closed ) AS c FROM ost_ticket WHERE year(closed) = '2013' AND monthname(closed) = 'January' AND source = '{$user}'  GROUP BY day LIMIT 30");
    $count = mysql_num_rows($q);
    return $count;
}
function test11($user)
{
    $q = mysql_query("SELECT * FROM ost_ticket WHERE closed LIKE  '%2013-06-   19%' AND source = '" . $user. "'");
       if(!$q)
    {
        echo mysql_error();
    }
 $r = 0;
$a = mysql_num_rows($q);
echo $a;
while($row = mysql_fetch_assoc($q))
{   
return $row['status'];
return $row['day'];
$r++;
}
}
 function getUser()
 {
    $q = mysql_query("SELECT * FROM ost_staff WHERE staff_id > 2");
    while($row = mysql_fetch_assoc($q))
    {
            $count1 = getRows($row['username']);
            $a = 0;
        echo '<div id="' . $row['username'] . '" class="stat">
        <span class="title">' . $row['username'] . '</span><br /><br />
        <div id="break"></div>';
        echo '
        <div id="data-' . $row['username'] .'">' . test11($row['username'])   . '</div>';
    echo '</div>';
    }
}

Thanks in advance for this.

Upvotes: 0

Views: 222

Answers (3)

Luigi Siri
Luigi Siri

Reputation: 2018

Here:

while($row = mysql_fetch_assoc($q))
{   
    return $row['status']; //Returns in only this value
    return $row['day'];
    $r++;
}

Fix:

$a = array()
while($row = mysql_fetch_assoc($q))
{   
    $a['status'][] =  $row['status'];
    $a['day'][] = $row['day'];
    //$r++;
}
return $a; //This will return an array with all the rows fetched.

Upvotes: 0

maialithar
maialithar

Reputation: 3123

You use return in while loop - your code exits the function when it encounters first return. This is probably the reason it doesn't work.

Upvotes: 1

Tsar Ioann
Tsar Ioann

Reputation: 444

A function exits on the first reached RETURN statement.

So, your function just returns one $row['status'];. You should put query results in an array and return it.

Upvotes: 2

Related Questions