d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > N Help With Update Script > Php/mysql
12Next
Add Reply New Topic New Poll
Member
Posts: 158
Joined: May 6 2011
Gold: 0.70
Aug 3 2012 02:37pm
Hello again dear programming community :) again I need some assistance with one of my .PHP files.

What I'm trying to accomplish is to run this file twice a day to update the rankings of our users websites. When I run the script I get the error "HTTP-feil 500 (Internal Server Error)"

Could anyone help me? Detect any spelling errors? Improve the coding? Or if possible, complete it.
If you need more information then please do ask.

Thanks a ton in advance!

Code
<?php
include('config2.php');

$sql = "SELECT * FROM sites ORDER BY views DESC";
$result = mysql_query($sql) OR die(mysql_error());
$total = mysql_num_rows($result);

echo"$total";
$i = 1;
while($i != ($total))
{
$update = ("UPDATE sites SET rank=$i WHERE rank='1'");
$result = mysql_query($update);
if($result)
 { echo"HURRA"; }
 else
 { echo"FEIL";}
 
 $i++
}


?>
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Aug 3 2012 04:41pm
You have MANY PHP errors in the script.
http://www.raymondhill.net/finediff/viewdiff.php?data=ade30190e015c888a4148925ec70ad5d0dc6282e
http://codepad.viper-7.com/jkEkR2

Make sure the script doesn't contain these errors before you paste it here, otherwise we will not be able to help you with the real reason that the script isn't working.
If the 500 error still occurs with a valid version of your code example, there are ALOT of information online on the issue already.
Member
Posts: 158
Joined: May 6 2011
Gold: 0.70
Aug 4 2012 03:49am
Quote (eagl3s1ght @ Aug 4 2012 12:41am)
You have MANY PHP errors in the script.
http://www.raymondhill.net/finediff/viewdiff.php?data=ade30190e015c888a4148925ec70ad5d0dc6282e
http://codepad.viper-7.com/jkEkR2

Make sure the script doesn't contain these errors before you paste it here, otherwise we will not be able to help you with the real reason that the script isn't working.
If the 500 error still occurs with a valid version of your code example, there are ALOT of information online on the issue already.


I'm terribly sorry for not noticing those minor mistakes :| Was late at night and we had changed the document tons of times to try different techniques.
Here's an updated code:
Code
<?php
include('config2.php');

$sql = "SELECT * FROM sites ORDER BY views DESC";
$result = mysql_query($sql) OR die(mysql_error());
$total = mysql_num_rows($result);

echo $total;


for ($i = 1; $i != $total; $i++)
{
$update = "UPDATE sites SET rank=$i ";
$result = mysql_query($update);
 
if($result)
{
 echo "It's working";
} else {
 echo "Syntax Error";
}


}


?>


We have got it "working" but our problem now is to set the rank PER site, so for the FOR have to start at the first row in the database and move to the next row for every pass.

Do you have any idea how to make something like that? In case you do, post your PayPal account with the code please, you deserve a donation :p
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 4 2012 04:27am
$i = 0;
while ($row = mysql_fetch_assoc($result)){
$i++;
$update = "UPDATE sites SET rank=$i where site_id=$row['site_id']" ;
$result = mysql_query($update);
}

i dont do php, but something to that effect
Member
Posts: 158
Joined: May 6 2011
Gold: 0.70
Aug 4 2012 05:26am
Quote (carteblanche @ Aug 4 2012 12:27pm)
$i = 0;
while ($row = mysql_fetch_assoc($result)){
$i++;
$update = "UPDATE sites SET rank=$i where site_id=$row['site_id']"  ;
$result = mysql_query($update);
}

i dont do php, but something to that effect


Hmmm, I wonder if using site_id is the right choice, because we use DESC by views to get the top ranked to be in row 1, that sites ID can be anything :| Will test if it though! I am also unsure if the $i = 0; and $i++ is working, because when I test, every sites rank get's set to 7 (we have 7 sites for testing)
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Aug 4 2012 05:34am


This post was edited by eagl3s1ght on Aug 4 2012 05:35am
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Aug 4 2012 06:22am
Quote (OshiroTG @ 4 Aug 2012 13:26)
Hmmm, I wonder if using site_id is the right choice, because we use DESC by views to get the top ranked to be in row 1, that sites ID can be anything :| Will test if it though! I am also unsure if the $i = 0; and $i++ is working, because when I test, every sites rank get's set to 7 (we have 7 sites for testing)


Carteblanches' code should work fine. Can you paste the complete script that you used when every sites rank got set to 7?
Member
Posts: 158
Joined: May 6 2011
Gold: 0.70
Aug 5 2012 01:29pm
Quote (eagl3s1ght @ Aug 4 2012 02:22pm)
Carteblanches' code should work fine. Can you paste the complete script that you used when every sites rank got set to 7?


<?php
include('config2.php');

$sql = "SELECT * FROM sites ORDER BY views DESC";
$result = mysql_query($sql) OR die(mysql_error());
$total = mysql_num_rows($result);

echo $total;


$i = 1;
while ($row = mysql_fetch_assoc($result)){
$i++;
$siteid = $row[site_id];
$update = "UPDATE sites SET rank=$i where site_id=$siteid" ;
$result = mysql_query($update);
}


?>

This "works" for the first site. First site get's set rank 1, but all the other sites get set to rank 7 :/ It's as if the WHILE runs, but does not update the RANK field on every pass, and updates every row except the first one when it is "finished" and $i=7

This is breaking me :')
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 5 2012 01:54pm
$i = 1;
while ($row = mysql_fetch_assoc($result)){
$i++;
$siteid = $row[site_id];
$update = "UPDATE sites SET rank=$i where site_id=$siteid" ;
$result = mysql_query($update);
}

that might be the problem. delete the variable on the bottom.

This post was edited by carteblanche on Aug 5 2012 01:56pm
Member
Posts: 158
Joined: May 6 2011
Gold: 0.70
Aug 13 2012 10:32am
Quote (carteblanche @ Aug 5 2012 09:54pm)
$i = 1;
while ($row = mysql_fetch_assoc($result)){
$i++;
$siteid = $row[site_id];
$update = "UPDATE sites SET rank=$i where site_id=$siteid" ;
$result = mysql_query($update);
}

that might be the problem. delete the variable on the bottom.


Tried to delete it, didn't do no good :(
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll