Cron Job [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Cron Job [Resolved]

Post 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>";
}
?>
Last edited by Epiales on Sat Oct 12, 2013 1:43 pm, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Cron Job

Post 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:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Cron Job

Post 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);
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Cron Job

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: Cron Job

Post by OldRod »

No, I think what Alexander19 is saying is you set up the $sql but never execute it
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Cron Job

Post 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!
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Cron Job

Post 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 :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”