FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Database/PHP/Cron Jobs [Resolved]





mattd8752
I am going to have a cron job loading a php page, but I am a bit lost making a php script to look under 1 field in a database of a single table, and subtract 100 from it. It should be able to withdraw how much to subtract from the field in every entry from another entry in a seperate table. I would pay frih$ for this... Unfortunately, all I can pay is 15frih$ I will install some scripts for you free if you make this 2 if you want, look in my siggy for the link to my script installation.
JustaMin
mattd8752 wrote:
I am going to have a cron job loading a php page, but I am a bit lost making a php script to look under 1 field in a database of a single table, and subtract 100 from it. It should be able to withdraw how much to subtract from the field in every entry from another entry in a seperate table. I would pay frih$ for this... Unfortunately, all I can pay is 15frih$ I will install some scripts for you free if you make this 2 if you want, look in my siggy for the link to my script installation.


I'm not to clear what you want to do, could you give some sort of pseudo-code example please?
mattd8752
ON LOAD (from specified ip address)-- open a database
open a table -- find the column "cash" in every entry subtract 100 from its value...
mathiaus
I'd use something like below. Wasnt sure about where you were the number to subtract from so that could need major fiddling, otherwise you should just be able to change databse names etc.

Code:
<?php

//connect to database
mysql_connect(localhost,$databaseusername,$databasepassword);
@mysql_select_db($databasedatabase) or die( "Unable to select database");

// Minus what from each member?
$query="SELECT minus_value FROM othertable WHERE id='1'";
$result=mysql_query($query);
$row = mysql_fetch_row($result);
$minuswhat = $row[0];

//get values of each member
$query="SELECT id, cash FROM members";
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while($i<=$num) {
$id = mysql_result($result,$i,"id");
$id[$i] = $id;
$cash = mysql_result($result,$i,"id");
$cash[$i] = $cash - $minuswhat;
$i++;
}

$i=0;
while($i<=$num) {
$query = "UPDATE members SET cash='$cash[$i]' WHERE id='$id[$i]'";
$result = mysql_query($query);
$i++;
}

// close connection
mysql_close();
?>
mattd8752
Sorry I don't quite get that... I know the final part but the minus what part is confusing... is it setting the amount to $minuswhat so I could find it in a txt file that could be modded...
mattd8752
<?php
$databaseusername = "mattd875_phpbb";
$databasepassword = "HIDDEN";
$databasename = "mattd875_phpbb";
//connect to database
mysql_connect(localhost,$databaseusername,$databasepassword);
@mysql_select_db($databasename) or die( "Unable to select database");
$addwhat = "50";
//get values of each member
$query="SELECT id, user_famguycsh FROM phpbb_users";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while($i<=$num) {
$id = mysql_result($result,$i,"id");
$id[$i] = $id;
$user_famguycsh = mysql_result($result,$i,"id");
$user_famguycsh[$i] = $user_famguycsh + $addwhat;
$i++;
}
$i=0;
while($i<=$num) {
$query = "UPDATE phpbb_users SET user_famguycsh='$user_famguycsh[$i]' WHERE id='$id[$i]'";
$result = mysql_query($query);
$i++;
}
// close connection
mysql_close();
?>


Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/cashadd.php on line 12

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/cashadd.php on line 15

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/cashadd.php on line 17

Got any idea what to do for that? I changed it to add for the moment
JustaMin
mattd8752 wrote:
<?php
$databaseusername = "mattd875_phpbb";
$databasepassword = "HIDDEN";
$databasename = "mattd875_phpbb";
//connect to database
mysql_connect(localhost,$databaseusername,$databasepassword);
@mysql_select_db($databasename) or die( "Unable to select database");
$addwhat = "50";
//get values of each member
$query="SELECT id, user_famguycsh FROM phpbb_users";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while($i<=$num) {
$id = mysql_result($result,$i,"id");
$id[$i] = $id;
$user_famguycsh = mysql_result($result,$i,"id");
$user_famguycsh[$i] = $user_famguycsh + $addwhat;
$i++;
}
$i=0;
while($i<=$num) {
$query = "UPDATE phpbb_users SET user_famguycsh='$user_famguycsh[$i]' WHERE id='$id[$i]'";
$result = mysql_query($query);
$i++;
}
// close connection
mysql_close();
?>


Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/cashadd.php on line 12

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/cashadd.php on line 15

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/cashadd.php on line 17

Got any idea what to do for that? I changed it to add for the moment


Code:

$databaseusername = "mattd875_phpbb";
$databasepassword = "HIDDEN";
$databasename = "mattd875_phpbb";
//connect to database
mysql_connect(localhost,$databaseusername,$databasepassword);
@mysql_select_db($databasename) or die( "Unable to select database");
$addwhat = "50";
//get values of each member
$query="SELECT id, user_famguycsh FROM phpbb_users";
$result=mysql_query($query);

$num=mysql_num_rows($result);
while ($row = mysql_fetch_array($result))
{
   extract($row);
   $value = $user_famguycsh + $addwhat;
   $query2 = "UPDATE phpbb_users SET user_famguycsh='$value' WHERE id=$id";
   $result2 = mysql_query($query2);
}


That should acheive what you want, but running queries inside loops isn't a great idea, might be best to look into an alternative.
mattd8752
can you show me the whole code, I am not sure where to put that in.
JustaMin
That replaces:

Code:
$databaseusername = "mattd875_phpbb";
$databasepassword = "HIDDEN";
$databasename = "mattd875_phpbb";
//connect to database
mysql_connect(localhost,$databaseusername,$databasepassword);
@mysql_select_db($databasename) or die( "Unable to select database");
$addwhat = "50";
//get values of each member
$query="SELECT id, user_famguycsh FROM phpbb_users";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while($i<=$num) {
$id = mysql_result($result,$i,"id");
$id[$i] = $id;
$user_famguycsh = mysql_result($result,$i,"id");
$user_famguycsh[$i] = $user_famguycsh + $addwhat;
$i++;
}
$i=0;
while($i<=$num) {
$query = "UPDATE phpbb_users SET user_famguycsh='$user_famguycsh[$i]' WHERE id='$id[$i]'";
$result = mysql_query($query);
$i++;
}


If you get the same errors then theres an error in the first query, you may have mis-spelled a table or column name.
mattd8752
I still get

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/addcsh.php on line 12

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/addcsh.php on line 15

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/addcsh.php on line 17
JustaMin
mattd8752 wrote:
I still get

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/addcsh.php on line 12

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/addcsh.php on line 15

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd875/domains/mattdsworld.frih.net/public_html/forum/addcsh.php on line 17


in that case you have an error in the query itself. Are you sure the database table exists and is spelled correctly and the columns are too? Is there actually any data in the table yet?
mattd8752
Justamin, can you look in phpmyadmin, I will let you in if you promise not to do any damage?
JustaMin
mattd8752 wrote:
Justamin, can you look in phpmyadmin, I will let you in if you promise not to do any damage?


If you like, PM me with the details though, don't post them here Wink
mattd8752
JustaMin wrote:
mattd8752 wrote:
Justamin, can you look in phpmyadmin, I will let you in if you promise not to do any damage?


If you like, PM me with the details though, don't post them here Wink


Thats a good idea Smile anyhow, I pmed the data to you.
JustaMin
OK, there's no colum called id in that phpbb_users, it's user_id. Try this (complete code):

Code:

<?
$databaseusername = "mattd875_phpbb";
$databasepassword = "HIDDEN";
$databasename = "mattd875_phpbb";
//connect to database
mysql_connect(localhost,$databaseusername,$databasepassword);
@mysql_select_db($databasename) or die( "Unable to select database");
$addwhat = "50";
//get values of each member
$query="SELECT user_id, user_famguycsh FROM phpbb_users";
$result=mysql_query($query);

$num=mysql_num_rows($result);
while ($row = mysql_fetch_array($result))
{
   extract($row);
   $value = $user_famguycsh + $addwhat;
   $query2 = "UPDATE phpbb_users SET user_famguycsh='$value' WHERE id=$user_id";
   $result2 = mysql_query($query2);
}
// close connection
mysql_close();
?>


Let me know if you get any joy with that.
mattd8752
DINGDINGDING... wrong... it runs without error, but doesn't add the cash..
mattd8752
http://mattdsworld.frih.net/forum/profile.php?mode=viewprofile&u=2 I am looking at my phpbb profile, and it didn't add the cash, I've always been at 504$... got any solutions?
JustaMin
Dohhh bit slow as it's Firday Night and festivities have begun!

replace this:

$query2 = "UPDATE phpbb_users SET user_famguycsh='$value' WHERE id=$user_id";

with this:

$query2 = "UPDATE phpbb_users SET user_famguycsh='$value' WHERE user_id=$user_id";

Laughing
mattd8752
It works!!! thanks! can you help me with something else if I pm it to you?
JustaMin
mattd8752 wrote:
It works!!! thanks! can you help me with something else if I pm it to you?


I can till my girlfriend gets in from work, then I will be commandeered to watch crap tv and drink and smoke all night Surprised Of course I can help you tommorrow if so.

Feel free to PM now if you like and i'll help you till she gets in, no guarentees though i'm no professional programmer!

btw, are you intedning this script to be run on a per user basis, ie when a user does something to cause it to update (in which case it will be horribly innefficient when you have loads of users), or is it to be run on a schedule to update every user's info at once (in which case it can be cron jobbed to run at like 3am server time when it won't matter so much)
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.