I set up a cron job to run the following script every 30 min, Basically this script selects article info from stories table where field showing=0 And it than counts votes related to that article, the votes are located in votes table. itemname_field in votes table is equal to id field in stories table.
If you need more information on database structure and voting system, I describe it in depth here:
Set a cron job to update article information depending on its vote values
<?php
mysql_connect("localhost","username","password") or die (mysql_error("There was an error in connection"));
mysql_select_db("database") or die (mysql_error("There was an error in connection"));
$query = "select sum(vt.vote_value) as vote_value, vt.item_name from Votes vt join stories st on st.id = vt.item_name and st.showing = 0 group by vt.item_name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
if($row['vote_value'] > 9) {
$showing = 1;
}
else if($row['vote_value'] < -9) {
$showing = 2;
}
else {
$showing = 0;
}
$query2 = "UPDATE `stories` SET `showing` = $showing WHERE `id` = '".$row['item_name']."'";
mysql_query($query2);
}
?>
Can anyone suggest the reason, why this script is not working? If everything is alright with script, could it be something in cron job that needs to be done?
EDIT: when I run script in browser it gives the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
Which is this line while($row = mysql_fetch_array($result))
The error message you quote means you have an error in your query. Run it though a mysql client to get more info, or echo
mysql_error()in php.In such cases
mysql_query()returns boolean false, you can use this for error handling.