Hello all. I am still very new when it comes to PHP and MYSQL, and I have a bit of a problem. I have a website that is connected to another site through its API, which I have permission to access. This site is for virtual pets that hatch and grow up by people viewing and clicking them. My site has a database with three tables. One is for "ER" pets that have 4 days or less to mature before dying. The second is for eggs (the first stage of growth), and the third is for hatchlings (the second stage before being fully grown).
I currently have a cron job which executes a PHP script every 10 minutes to keep these databases clean. It works, but it takes an extraordinarily long amount of time to do so (when I tested the script directly, it took 10 minutes or more to complete). I really want to optimize this script to streamline things and make it more efficient.
<?php
$username="*******";
$password="*******";
$database="*******";
$host="********";
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
function update($type)
{
$query="SELECT * FROM $type";
$result=mysql_query($query);
$num=mysql_numrows($result);
$sql = mysql_query("SELECT Code FROM $type LIMIT $num");
while($object = mysql_fetch_object($sql))
{
$codevar = $object->Code;
$data = unserialize(file_get_contents("http://dragcave.net/**********/view/$codevar"));
$data = array_pop($data['dragons']);
$code=$data['id'];
$egg=$data['hatch'];
$hidden=$data['start'];
$views=$data['views'];
$unique=$data['unique'];
$clicks=$data['clicks'];
$hours=$data['hoursleft'];
$querynull = "DELETE FROM NICU WHERE Code=''";
mysql_query($querynull);
if($hidden == "0") //if hidden
{
$query = "DELETE FROM NICU WHERE Code='$code'";
$query1 = "DELETE FROM Eggs WHERE Code='$code'";
$query2 = "DELETE FROM Hatchlings WHERE Code='$code'";
mysql_query($query);
mysql_query($query1);
mysql_query($query2);
}
elseif($hours == -2) //if dead
{
$query = "DELETE FROM NICU WHERE Code='$code'";
$query1 = "DELETE FROM Eggs WHERE Code='$code'";
$query2 = "DELETE FROM Hatchlings WHERE Code='$code'";
mysql_query($query);
mysql_query($query1);
mysql_query($query2);
}
elseif($hours == -1) //if adult or frozen
{
$query = "DELETE FROM NICU WHERE Code='$code'";
$query1 = "DELETE FROM Eggs WHERE Code='$code'";
$query2 = "DELETE FROM Hatchlings WHERE Code='$code'";
mysql_query($query);
mysql_query($query1);
mysql_query($query2);
}
if($egg == "0" && $hours > 96) //if egg
{
$query = "INSERT INTO Eggs VALUES ('','$code')";
mysql_query($query);
$status="$code is in the hatchery.";
}
elseif($egg != "0" && $hours > 96) //if hatchling
{
$query1 = "DELETE FROM Eggs WHERE Code='$code'";
mysql_query($query1);
$query = "INSERT INTO Hatchlings VALUES ('','$code')";
mysql_query($query);
$status="$code is in the nursery.";
}
if($hours > 96) //if not ER
{
$query = "DELETE FROM NICU WHERE Code='$code'";
mysql_query($query);
}
elseif($hours <= 96 && $hours != -2 && $hours != -1) //if ER
{
$query = "INSERT INTO NICU VALUES ('','$code')";
mysql_query($query);
$status="$code is in the ER.";
}
}
}
update('NICU');
update('Eggs');
update('Hatchlings');
?>
This is what the script looks like right now.
Whenever it runs, I want it to comb through the databases and make sure everything is in the correct table. I want anything with 4 days or less to be in NICU. Anything that has hatched should be moved to the Hatchlings table, and anything that has grown, died, or is hidden should be removed from the site. All of this information is taken from the other site's API.
There is probably a much better way to go about it than what I have, but with my limited knowledge it was the best I could come up with. So what I'm asking is just some advice on how I can streamline this script. Or maybe a way I can completely rethink my process to make it as easy and efficient as possible.
Thank you for your help. π