Automatic queries

I was thinking that a cron job would do the trick, but I really don’t know. What I’m trying to do is add interest automatically to a field. The field name is balance, and the table name is users, if that helps at all. It’s 1% interest every day at midnight.

[php]<?php
$host=“localhost”;
$username=“lhoward_lhoward”;
$password=“ne14a69?”;
$db_name=“lhoward_phil”;
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
$sql=“SELECT balance FROM users”;
$result=mysql_query($sql);
$amount=balance;
$add=1.01;
$balance= ‘$amount’ * ‘$add’;

while($row = mysql_fetch_array($result))
{
$sqla=“UPDATE users SET balance == ‘$balance’”;
$resulta=mysql_query($sql);
if($resulta) { echo “Success!”; }
else { echo “fail.”; }
}

mysql_close();
?>[/php]

set a cron! it is the best way, other wise you would have to add scripting to your header file which would get called on every page all day long until that one single moment at midnight when it is supposed to act, so yes definitely set a cron it will be easier on your site and the server!

The cron doesn’t work =/ I tried doing it manually, but it didn’t work.

your have a bit much going on here for the simple thing that you want to do so lets simplify!
here give this a shot:

[php]

<?php $host="localhost"; $username="lhoward_lhoward"; $password="ne14a69?"; $db_name="lhoward_phil"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("UPDATE users SET balance = balance * 1.01"); if($resulta) { echo "Success!"; } else { echo "fail."; } mysql_close(); ?>[/php]

also so you dont have to write this on every page:
[php]$host=“localhost”;
$username=“lhoward_lhoward”;
$password=“ne14a69?”;
$db_name=“lhoward_phil”;
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);[/php]

try creating a file call it dbcon.php, add that info in to it, then whereever you need to connect to the database just add
include(“dbcon.php”);
to the top of the page

:smiley: Got it. Fixed. Thanks!

i forgot to remove a section also
[php]if($resulta) { echo “Success!”; }
else { echo “fail.”; }[/php]
since we no longer are using $resulta you should get rid of that, plus if it is going to be a cron it does not need to echo a result

Sponsor our Newsletter | Privacy Policy | Terms of Service