php mysql update via cron - php

I have a php file that I run via cron. For testing I've set it to run every hour, but normally it will run once a day. What I want is to make it so that if the player has the autotechrefinery field set to 1, then they should have their population pulled from the table, how much money, research supplies, and technology they have.
From there, what I want is to deduct 500 researchsupplies, add 50 technology, and deduct 10% of population from money. All of this seems to be working when I run the file manually, but when I let it run automatically via cron, there is something that isn't working that as a result it sets technology to 50, researchsupplies to -500 and money to 0.
$refCheck = mysql_query("SELECT * FROM players WHERE autotechrefinery='1'");
while($rC = mysql_fetch_array($refCheck)) {
$nation = $rC['nation'];
$pop = $rC['population'];
$rsupplies = $rC['researchsupplies'];
$cash = $rC['money'];
$tech = $rC['technology'];
$newtech = $tech+50;
$newmoney = $cash-($pop*.1);
$newsupplies = $rsupplies-500;
mysql_query("UPDATE players SET money='$newmoney', technology='$newtech', researchsupplies='$newsupplies', techbought='1' WHERE autotechrefinery='1'"); }

The bug in your code is: the UPDATE will always update all entries. So all entries get the value assigned of the last pass of your loop.
To fix the code you need some ID to identify the row and use it in the update:
$refCheck = mysql_query("SELECT * FROM players WHERE autotechrefinery='1'");
while($rC = mysql_fetch_array($refCheck)) {
$ID = $rC['id'];
$nation = $rC['nation'];
$pop = $rC['population'];
$rsupplies = $rC['researchsupplies'];
$cash = $rC['money'];
$tech = $rC['technology'];
$newtech = $tech+50;
$newmoney = $cash-($pop*.1);
$newsupplies = $rsupplies-500;
mysql_query("UPDATE players SET money='$newmoney', technology='$newtech', researchsupplies='$newsupplies', techbought='1' WHERE id="$ID');
}
You didn't ask for improvements but you could achieve the same result with one SQL statement:
UPDATE players set money=money - population * .1, technology = technology + 50, researchsupplies = researchsupplies - 500;

Related

Stopping a PHP loop when updating MySQL database

I'm creating a basic main menu for a stock market simulator where the price of a company will be updated periodically. For testing purposes, I need to make a loop to display the price of a share on the website five times (with the website automatically updating without refreshing) and to update the database at the same time.
I have successfully wrote some code which will both update the database with the current share price and will also update the website as well. However, when I have tried to include a loop I have come to a problem. I have included a loop to iterate five times but the problem that I am having is that the code continues to iterate even after five tries.
PHP:
<?php
$conn = mysqli_connect("localhost", "root", "", "prices");
if ($conn->connect_error)
{
die("Connection error: ". $conn->connect_error);
}
$result = $conn->query("SELECT `price` FROM `priceTable` WHERE `company` = 'Bawden'");
$x = 0;
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
echo $row['price'];
echo '<br><br>';
echo $x;
if ($x < 5)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
$x++;
}
}
}
?>
The above code will be displayed in a separate document with Javascript code and I can post this if required in the original post however I originally chose not to as I believe this is a PHP only problem. I have chosen to display $x to see if the value will increment. However, when running, the value of $x will stay at 0.
My expected result is that, on the website, there will only be five different updates and in the database, the database will only be updated five times.
However, my actual result is that the website and database are both continuously being updated, not stopping after five times.
I'm trying to limit the update command to only 5 updates yes. At the
moment, for testing purposes, there is only one company in my database
with one price only. So I'm updating this one company's price five
times
If you need to do the update 5 times for each row returned from the database, change your if statement to a for loop. Change this :-
if ($x < 5)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
$x++;
}
to this
for ($x = 0, $x < 5, $x++)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
}
This will repeat the process exactly 5 times and not rely on a separate counter (remove the other references to $x). Not sure why you would want to update the same record 5 times with different random values though.
The else will break the first loop, the second one will stop on the first while loop.
while ($row = $result->fetch_assoc())
{
echo $row['price'];
echo '<br><br>';
echo $x;
if ($x < 5)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
$x++;
}else{
break;
}
break;
}
What makes you think the loop should stop after 5 iterations?
You need to add the condition $x<5 in the while ($row = $result->fetch_assoc())
Edit following your comment
What you initially wrote is something like loop hundreds of times if need be and do something in the first 5 occurrences (starting loop 6, keep looping but do nothing).
Now for the 2nd half of your comment, I'm not sure what you mean.
What I see in your code is:
Select all prices for company = 'Bawden'
Update all the prices for company = 'Bawden' 5 times (loop) with a random value, the same one, on all the records.
Not enough information to tell for sure but I don't think it makes sense: on one hand, you except to have several records under company = 'Bawden (= reason why you created a loop), on the other hand, your update feels like it is written under the assumption there would be 1 record only...
Are you missing something like a price date from your table? What is the primary key of priceTable?
Try to post more technical details about your table (definition, sample of data) or it will be complicated to help further.

Recursive call for cron job

I have a three PHP script, from which two are running as a separate cron job each day in server.
First script(get_products.php), make curl call to external server to get all products data and stored that data in database. Note that there are around 10000 products and increasing each day.
In Second script(find_related.php), selects products from database stored by first script, perform some operations and store operational data in another database. Each product have 10 rows so in this database there is around 100000 rows. This script is running as cron. Sometimes the script is not executed fully and that's why, the actual and expecting results are not stored in database. I included this line of code in script: ini_set('max_execution_time', '3600');
But it not works.
Here is the process done in this script:
Normally task is to find 10 related products based on tags. I have around 10300 products stored in my DB. Each time query take one product and their tags and try to randomly find one product tagged with same tag as main product and store the related product data into another DB for third script. Only one product per tag is allowed. If it will not find total of 10 related products then randomly gets products from another DB named bestseller_products.
Here is my code:
$get_all_products = mysql_query('SELECT * FROM store_products');
while($get_products_sql_res = mysql_fetch_array($get_all_products)){
$related_products = array();
$tags = explode(",",$get_products_sql_res['product_tags']);
$product_id = $get_products_sql_res['product_id'];
$product_handle = $get_products_sql_res['product_handle'];
$get_products_sql = mysql_query('SELECT * FROM related_products WHERE product_handle="'.$product_handle.'"');
if (mysql_num_rows($get_products_sql)==0)
{
$count = 0;
foreach($tags as $t){
$get_related_products_sql = mysql_query("SELECT product_handle, product_title, product_image FROM store_products WHERE product_tags like '%".$t."%' AND product_id != '".$product_id."' ORDER BY RAND()");
if(!$get_related_products_sql){
continue;
}
while($get_related_products = mysql_fetch_array($get_related_products_sql) ){
$related_product_title = mysql_real_escape_string($get_related_products['product_title']);
$found = false;
foreach($related_products as $r){
if($r['handle'] == $get_related_products['product_handle']){
$found = true;
break;
}
}
if($found == false){
$related_products[$count]['handle'] = $get_related_products['product_handle'];
mysql_query("INSERT INTO related_products (product_handle, product_id, related_product_title, related_product_image, related_product_handle) VALUES ('$product_handle','$product_id','$related_product_title', '$get_related_products[2]', '$get_related_products[0]')");
$count = $count + 1;
break;
}
}
}
if($count < 10){
$bestseller_products = mysql_query("SELECT product_handle, product_title, product_image FROM bestseller_products WHERE product_id != '".$product_id."' ORDER BY RAND() LIMIT 10");
while($bestseller_products_sql_res = mysql_fetch_array($bestseller_products)){
if($count < 10){
$found = false;
$related_product_title = mysql_real_escape_string($bestseller_products_sql_res['product_title']);
$related_product_handle = $bestseller_products_sql_res['product_handle'];
foreach($related_products as $r){
if($r['handle'] == $related_product_handle){
$found = true;
break;
}
}
if($found == false){
$related_product_image = $bestseller_products_sql_res['product_image'];
mysql_query("INSERT INTO related_products (product_handle, product_id, related_product_title, related_product_image, related_product_handle) VALUES ('$product_handle','$product_id','$related_product_title', '$related_product_image', '$related_product_handle')");
$count = $count + 1;
}
}
}
}
}
}
Third script(create_metafields.php), created metafields in external server using data created by second script. And same problem arises as in second script.
So i want to execute the second script into parts. I mean, not to process all 10000 products in one call but want to run unto parts(1-500,501-1000,1001-1500,..) like it. But dont want to create separate cron jobs. Please suggest if someone has solution. I really need to figure it out.
Thanks in advance!

Improve performance when copying records from table to another one

Hi buddies :) I was required to create a php code to handle some workers' data stored in DB. I got the desired result but it takes seconds and seconds (seconds and seconds! >.<) to finish, so I'm afraid I'm not doing something in a right way :(
The workers' data is stored in a mysql table (table1), something like this:
I'm given a pair of dates: initial_date (a) and final_date (b), so my goal is to copy the given workers' data in a new table (table2), day by day from a to b. The expected table should be as shown below (this table will be used later as a basis for further operations, which is not part of the question)
It's a requirement to overwrite any existing data between a and b dates, and 'jump' weekends and holidays.
To get my goal, I'm coding this (let's assume that the connection and all that is done and the checkworkingday function is given):
$initialdate = '2016-10-10';
$finaldate = '2016-10-12';
$x = $initialdate;
do {
if (checkworkingday($x) == true) {
$query = mysqli_query($connection,"SELECT name,task FROM table1");
while($row = mysqli_fetch_array($query)) {
$task = $row['task'];
$worker = $row['name'];
$query2 = mysqli_query($connection,"SELECT task FROM table2 WHERE name = '$worker' AND date = '$x'");
$row2 = mysqli_fetch_array($query2);
$existingtask = $row2['task'];
if (!isset($existingtask)) {
mysqli_query($connection,"INSERT INTO table2 (date,name,task) VALUES('".$x."','".$worker."','".$task."')");
} else {
mysqli_query($connection,"UPDATE table2 SET task = '".$task."' WHERE date = '".$x."' AND worker = '".$name."'");
}
}
}
$x = date('Y-m-d', strtotime($x . "+1 day"));
} while ($x <= $finaldate);
Just for 3 days as shown in the example, it takes a long to end; and for several weeks or months it takes very, very long (even max execution time is exceeded depending on dates range!).
I'm a newbie and I know the code is quite 'rustic', but I've revised and checked the code and info out there without getting a better performance. What am I doing wrong? Thanks :)
Instead of looping through the enitre data, try INSERT.. SELECT :
INSERT INTO table2 (date,name,task)
SELECT date,name,task
FROM Table1
WHERE < >;

PHP / MySQLI won't update properly

PHP / MySQLI
$query = $conn->query(
"SELECT
cart.cart_id,
cart.user_ip,
cart.cart_amount,
cart.fk_product_id,
products.product_id,
products.prod_amount
FROM
cart
INNER JOIN
products
ON
cart.fk_product_id = products.product_id
WHERE
fk_product_id = '{$productID}' AND user_ip = '{$ip}'
");
$item = $query->fetch_assoc():
$FKproductID = $item['fk_product_id'];
$FKproductID = iSQLsecure($objConnection, $FKproductID);
$FKproductID = trim($FKproductID);
$CartAmount = $item['cart_amount'];
$CartAmount = iSQLsecure($objConnection, $CartAmount);
$CartAmount = trim($CartAmount);
$ProductAmount = $item['prod_amount'];
$ProductAmount = iSQLsecure($objConnection, $ProductAmount);
$ProductAmount = trim($ProductAmount);
$one = 1;
$add_amount = $CartAmount + $one;
$remove_amount = $ProductAmount - $one;
if($FKproductID == $productsID) {
$update_cart_item = $objConnection->query("UPDATE cart SET cart_amount = '{$add_amount}' WHERE fk_product_id = '{$productID}' AND user_ip = '{$ip}'");
$update_product_amount = $objConnection->query("UPDATE products SET prod_amount = '{$remove_amount}' WHERE product_id = '{$productID}'");
} else {
$add_item = $objConnection->query("INSERT INTO cart (user_ip, add_amount, fk_product_id) VALUES ('{$ip}', '{$add_amount}', '{$productID}')");
$update_product_amount = $objConnection->query("UPDATE products SET prod_amount = '{$remove_amount}' WHERE product_id = '{$productID}'");
}
$objConnection->close();
My Problem
$update_product_amount = $objConnection->query("UPDATE products SET prod_amount = '{$remove_ProductAmount}' WHERE product_id = '{$productID}'");
I use this two places if($FKproductID == $productsID) { /*HERE1*/ } else { /*AND HERE2*/}
For some reason it works in "HERE1" but not in "HERE2"?
If item is not in cart
It will insert it into my "cart" database and update "products" amount value. It is supposed to remove 1 from the current product amount (10 becomes 9). Instead it turns 10 into -1.
If item is in cart
It updates perfectly and turns 10 into 9. The code is the exact same but gives two different results?
I'll assume the above code isn't complete, since some variable name is inconsistent ($remove_amount vs $remove_ProductAmount) and yet your code run fine on some case.
For the queries, why don't you just subtract the amount on update query? Something like
UPDATE table SET field = field - 1 WHERE x = y
Even if the number 1 is dynamic, it's safer to generate the number on variables then cast it to integer. On worst case, it would be converted to zero (which can be easily added on your if() as additional validation).
I also recommend using debugger. Something like Kint or PsySH would suffice if you don't want to trouble yourself with setting up XDebug.
I think the issue may be with your INNER JOIN. From what I can tell, if the item isn't in your cart, then the cart.fk_product_id won't exist, and therefore the query will return an empty object.
If you have error display turned off (likely in the php.ini with display_errors = Off), non-existant array keys will be treated as null during equations.
When it comes time to do the equations, php will convert null to integer(0) which results in 0 - 1 = -1 if it fails the if statement.
Have you tried to output the returned object of the query? You can do this by using:
echo "<pre>"; print_r($item); echo "</pre>";
(the <pre> tag makes it a bit more readable).
Also, check the status of your httpd/apache error log if you have log_errors turned on.

MySQL queries optimization

So I've been working on an IRC game for one year now, written in PHP and using a PHP to IRC framework.
Recently, I've added the ability to archive scores (they're being reseted every couple hundreds of games) which forced me to update various admin functions.
I've just updated a function that allows me to merge two players (some users don't bother looking for their old password etc...) in order to merge archived scores too (in case a reset has occurred before I find the duplicated accounts).
The score-merging part (below) works has intended, but I'm wondering if I can optimize the process because I find it rather heavy (but can't think of something better) :
$from_stats = $this->db->query("SELECT `games`, `wins`, `points`, `date_archive` FROM ".$this->dbprefix."score WHERE `id`=".$id1." AND `channel`='".$gamechan."' GROUP BY `date_archive`"); // get scores for the original account
$to_stats = $this->db->query("SELECT `games`, `wins`, `points`, `date_archive` FROM ".$this->dbprefix."score WHERE `id`=".$id2." AND `channel`='".$gamechan."' GROUP BY `date_archive`"); // get scores for the duplicated account
$from_games = array();
$from_wins = array();
$from_points = array();
$from_date = array();
while (list($fromstats_games,$fromstats_wins,$fromstats_points,$fromstats_date) = $this->db->fetchRow($from_stats)) { // build score arrays for the original account
$from_games[count($from_games)] = $fromstats_games;
$from_wins[count($from_wins)] = $fromstats_wins;
$from_points[count($from_points)] = $fromstats_points;
$from_date[count($from_date)] = $fromstats_date;
}
$to_games = array();
$to_wins = array();
$to_points = array();
$to_date = array();
while (list($tostats_games,$tostats_wins,$tostats_points,$tostats_date) = $this->db->fetchRow($to_stats)) { // build score arrays for the duplicated account
$to_games[count($to_games)] = $tostats_games;
$to_wins[count($to_wins)] = $tostats_wins;
$to_points[count($to_points)] = $tostats_points;
$to_date[count($to_date)] = $tostats_date;
}
foreach ($from_date as $key1 => $id1_date) {
foreach ($to_date as $key2 => $id2_date) {
if ($id1_date == $id2_date) { // merge scores if dates match
$from_games[$key1] += $to_games[$key2];
$from_wins[$key1] += $to_wins[$key2];
$from_points[$key1] += $to_points[$key2];
$this->db->query("UPDATE ".$this->dbprefix."score SET `games`=".$from_games[$key1].", `wins`=".$from_wins[$key1].", `points`=".$from_points[$key1]." WHERE `id`=".$id1." AND `channel`='".$gamechan."' AND `date_archive`='".$id1_date."'");
break;
}
}
}
$this->db->query("DELETE FROM ".$this->dbprefix."score WHERE `id`=".$id2); // delete all entries for the duplicated account
Just one tip: after all use this query (if You have appropriate privilages)
$this->db->query("OPTIMIZE TABLE ".$this->dbprefix."score");
This should cause all indexes in this table to be recalculated. You'll notice the index file size has changed to 1kb (or few bytes)

Categories