Computer Science Canada

sends 71 emails instead of 6?!!

Author:  gsquare567 [ Sat Apr 12, 2008 4:06 pm ]
Post subject:  sends 71 emails instead of 6?!!

it is supposed to send 5 emails to the top 5 monthlyscore users and then one to me. the one to me sends a list of 70 though, and there are two of each name. the usernames are all single characters like A or 0 or even blank. there must be a mysql error in my query, but there shouldnt be because i specifically say LIMIT 5!

code:
<?php

include "../cfg.php";

$result = $db->query("
                SELECT * FROM members
                ORDER BY `monthlyscore` DESC
                LIMIT 5;
        ");// get winners
       
$monthly_winners = mysql_fetch_array($result);

$winners_list = "";
$i = 0;
foreach ($monthly_winners as $monthly_winner)
{
        $i++;
        $winners_list.= "Monthly Rank ".$i.": ".$monthly_winner['username']." with ".$monthly_winner['monthlyscore']." points!\n";

        // email every winner
        mail("Admin@VIPortals.com", "VIPortals - Congradulations on Winning, ".$monthly_winner['username']."!!!",
        "Hello ".$monthly_winner['username'].",\n\nCONGRADULATIONS!!! You are one of this month's Monthly VIPortals $$$ Winners!\n\n
        Your Monthly Rank is ".$i." with ".$monthly_winner['monthlyscore']." points!\n\n
        In order to receive your winnings, simply reply to this email your PayPal Email Address, and you will receive your winnings as soon we get them. Your patience is required, and if you disobey any of our rules, found on our site found at 'http://www.viportals.com/index.php?params=page/5/', we will cancel your payment.\n\n
        Please understand that your winnings will depend solely on the site's profit and your ranking.\n\n
        Thank you very much for being such an active participant on my site. I hope to see more of you, and wish you the best of luck in the new month's contest!\n\n
        Regards,\n\n
        The VIPortals Admin",
        "From: Admin@VIPortals.com");
}
                       
// send top 10 to my email
mail("Admin@VIPortals.com", "Monthly VIPortals $$$ Winners", $winners_list, "From: Admin@VIPortals.com");

// update player monthly scores
$db->query("UPDATE members
        SET `lastmonthscore`=`viportalsscore`,
        `monthlyscore`=0
        WHERE 1=1");

?>

Author:  btiffin [ Sat Apr 12, 2008 5:54 pm ]
Post subject:  Re: sends 71 emails instead of 6?!!

Excuse the lack on knowledge, PHP is not my thing.

mail takes a string TO, a string SUBJECT and a string MESSAGE and then the optionals. You are passing an array variable as the message. I'm not sure how that will be parsed by the mail() function, but it seems wrong. You may need to reformat $winners_list into a $winners_list(0) linefeed $winners_list(1) linefeed. I'm sure a true PHP'er will have better advice.

Plus; I could be completely out to lunch and should probably have stayed quiet.

Cheers

Author:  gsquare567 [ Sat Apr 12, 2008 10:44 pm ]
Post subject:  RE:sends 71 emails instead of 6?!!

sorry, but i think that you did not look carefully enough.

code:
$result = $db->query("
      SELECT * FROM members
      ORDER BY `monthlyscore` DESC
      LIMIT 5;
   ");// get winners
   
$monthly_winners = mysql_fetch_array($result);

$winners_list = "";
$i = 0;
foreach ($monthly_winners as $monthly_winner)


$winners_list is a string. it is not the problem.

the problem is $monthly_winners, which is an array created from the MySQL query. for some reason, the loop runs 70 times.

Author:  Dan [ Sat Apr 12, 2008 10:55 pm ]
Post subject:  RE:sends 71 emails instead of 6?!!

What happens if you do the SQL query directly on your data base and not threw php?

If you have command line access to the mysql server or phpadmin you could check if it returns the right amount of rows and then find out if it is your SQL or your php code.

Author:  btiffin [ Sat Apr 12, 2008 11:27 pm ]
Post subject:  Re: sends 71 emails instead of 6?!!

Yeah, sorry; This is not my expertise at all.

So I'll ask a question now. In double quoted strings what does $$$ return? What does the "Monthly VIPortals $$$ Winners" expand to after string parsing?

Excuse the interruption. Should have stayed quiet. Not that I mind looking out to lunch, love learning things. Smile

Author:  gsquare567 [ Sun Apr 13, 2008 12:07 am ]
Post subject:  RE:sends 71 emails instead of 6?!!

i've tried it in my PHPMyAdmin SQL section and it returns the top 5 as wanted.

Author:  Dan [ Sun Apr 13, 2008 12:13 am ]
Post subject:  RE:sends 71 emails instead of 6?!!

I think you might be using the mysql_fetch_array command incorectly.

You should take a look at: http://ca3.php.net/mysql_fetch_array

It seems to be that mysql_fetch_array returns an array of the coloums not an array of rows so you are looping threw each coloumn rather then each row. If you look at the examples in the link i gave you, you can see the proper method of looping threw the rows rather then the colomums.


: