Page 1 of 1

Cron Job [Resolved]

Posted: Sat Oct 12, 2013 10:37 am
by Epiales
Okay, for some reason my cronjob stopped working when I switched to mysqli. Anyone see the problem? I've looked and tampered with for an hour or so. It's supposed to update the rankings, but isn't. Makes it hard to fix when there is no visible error :evil: :evil:

cronjob

Code: Select all

<?php
$get_attack = "SELECT `id`,`attack` FROM `stats` ORDER BY `attack` DESC";
    $i = 1;
$rank = array();
$get_attack = mysqli_query($db_conx, $get_attack);
$numrows = mysqli_num_rows($get_attack);
while ($attack = mysqli_fetch_array($get_attack, MYSQLI_ASSOC)) {
$rank[$attack['id']] = $attack['attack'];
$sql = "UPDATE `ranking` SET `attack`='".$i."' WHERE `id`='".$attack['id']."'";
    $i++;
}

$get_defense = "SELECT `id`,`defense` FROM `stats` ORDER BY `defense` DESC";
    $i = 1;
$get_defense = mysqli_query($db_conx, $get_defense);
$numrows = mysqli_num_rows($get_defense);
while ($defense = mysqli_fetch_array($get_defense, MYSQLI_ASSOC)) {
$rank[$defense['id']] += $defense['defense'];
$sql = "UPDATE `ranking` SET `defense`='".$i."' WHERE `id`='".$defense['id']."'";
    $i++;
}

asort($rank);
$rank2 = array_reverse($rank,true);
$i = 1;
foreach($rank2 as $key => $val){
$sql = "UPDATE `ranking` SET `overall`='".$i."' WHERE `id`='".$key."'";
    $i++;
}
?>
It is not working at all. Doesn't pull the players attack or defense and list it like it should :( :(

Here is my page that calls the information:

Code: Select all

<?php
        
$sql = "SELECT `id`,`overall` FROM `ranking` WHERE `overall`>'0' ORDER BY `overall` ASC";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td align='center'><font color='white'>" . $row['overall'] . "</font></td>";

$get_user = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
$rank_name = mysqli_fetch_assoc($get_user);
echo "<td align='center'><a href=\"mugstats.php?id=" .$row['id']."\"><font color='white'>" . $rank_name['username'] . "</font></a></td>";

$get_gold = "SELECT `gold` FROM `stats` WHERE `id`='".$row['id']."'";
$rank_gold = mysqli_fetch_assoc($get_gold);
echo "<td align='center'><font color='white'>" . number_format($rank_gold['gold']) . "</font></td>";
echo "</tr>";
}
?>

Re: Cron Job

Posted: Sat Oct 12, 2013 12:30 pm
by Epiales
Okay, update. I added some rankings in the database and it's showing the rankings in the ranking page. So it must be the actual cronjob that's not working correctly. And I still haven't figured out why :cry: :cry: :cry:

Re: Cron Job

Posted: Sat Oct 12, 2013 1:09 pm
by alexander19

Code: Select all

$sql = "UPDATE `ranking` SET `attack`='".$i."' WHERE `id`='".$attack['id']."'";
$sql = "UPDATE `ranking` SET `defense`='".$i."' WHERE `id`='".$defense['id']."'";
shouldn't you query that?

Code: Select all

mysqli_query($db_conx, $sql);

Re: Cron Job

Posted: Sat Oct 12, 2013 1:21 pm
by Epiales
alexander19 wrote:

Code: Select all

$sql = "UPDATE `ranking` SET `attack`='".$i."' WHERE `id`='".$attack['id']."'";
$sql = "UPDATE `ranking` SET `defense`='".$i."' WHERE `id`='".$defense['id']."'";
shouldn't you query that?

Code: Select all

mysqli_query($db_conx, $sql);
I could, or I could use the or die statement, but with those it doesn't work either. My original one written in mysql had the or die statement, but I don't think I need it with the way it's written. But I added your suggestion, and didn't work either. There are no errors, but the cronjob still doesn't work :( :( :( Thank you for the help.

Re: Cron Job

Posted: Sat Oct 12, 2013 1:32 pm
by OldRod
No, I think what Alexander19 is saying is you set up the $sql but never execute it

Re: Cron Job

Posted: Sat Oct 12, 2013 1:33 pm
by Epiales
Okay, figured it out...

$user_query = mysqli_query($db_conx, $sql);

I needed to query the other two with different query command.

Code: Select all

$user_query = mysqli_query($db_conx, $sql);
$user_query1 = mysqli_query($db_conx, $get_user);
$user_query2 = mysqli_query($db_conx, $get_gold);
Thanks for the reading and helping ;)

Making the code:

Code: Select all

$sql = "SELECT `id`,`overall` FROM `ranking` WHERE `overall`>'0' ORDER BY `overall` ASC";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td align='center'><font color='white'>" . $row['overall'] . "</font></td>";


$get_user = "SELECT `username`, `id` FROM `users` WHERE `id`='".$row['id']."'";
$user_query1 = mysqli_query($db_conx, $get_user);
$numrows = mysqli_num_rows($user_query1);
$rank_name = mysqli_fetch_assoc($user_query1);
echo "<td align='center'><a href=\"mugstats.php?id=" .$row['id']."\"><font color='white'>" . $rank_name['username'] . "</font></a></td>";


$get_gold = "SELECT `gold` FROM `stats` WHERE `id`='".$row['id']."'";
$user_query2 = mysqli_query($db_conx, $get_gold);
$numrows = mysqli_num_rows($user_query2);
$rank_gold = mysqli_fetch_assoc($user_query2);
echo "<td align='center'><font color='white'>" . number_format($rank_gold['gold']) . "</font></td>";
echo "</tr>";
}
 
Works like a charm!

Re: Cron Job

Posted: Sat Oct 12, 2013 1:38 pm
by Epiales
OldRod wrote:No, I think what Alexander19 is saying is you set up the $sql but never execute it
Alexander was right as well. I had put the "mysqli_query($db_conx, $sql);" before I made the query statements, so it worked. I removed them and it didn't work. Both the query and exectution as Alexander stated is what made it work. YAY

So thanks Alexander :)