I've written a few simple functions to admin a 5 latest added companies table in a database, and I've discovered a weird error.
If I call the check_new_companies_table() anywhere it works fine, but calling it from within the add_to_new_companies() function, if I test output $newCompanyRows then it comes out blank... here's the code
$newCompanyRows=5;
function add_to_new_companies($companyID, $companyName, $categoryID)
{
global $db;
$db->safe_query("DELETE FROM newcompanies ORDER BY timeadded LIMIT 1");
$q="INSERT INTO newcompanies select $companyID, '$companyName', cats.name, $categoryID, NULL
FROM cats WHERE cats.id=$categoryID LIMIT 1";
$db->safe_query($q, "Error adding to newcompanies: $q");
check_new_companies_table();
}
// makes sure the new companies table has $newCompanyRows entries
function check_new_companies_table()
{
global $db, $newCompanyRows;
$db->safe_query("SELECT count(*) FROM newcompanies");
$row=$db->fetch_row();
$numRows=$row[0];
echo 'Limit Table to '.$newCompanyRows.' entries<br>';
//above line prints 5 if function called outside the above function, but empty when called from it
if($numRows > $newCompanyRows)
{
echo "TOO MANY NEWCOMPANY ROWS!<br>";
$limit=$numRows-$newCompanyRows;
$q="DELETE FROM newcompanies ORDER BY timeadded LIMIT $limit";
$db->safe_query($q);
} // if there aren't enough rows add the companies with the highest expire date (could do rand)
elseif($numRows < $newCompanyRows)
{
echo "NOT ENOUGH NEW COMPANY ROWS!";
$limit=$newCompanyRows - $numRows;
$q="INSERT INTO newcompanies SELECT co.id, co.name, cats.id, cats.name, 0
FROM companies co, cats WHERE co.categoryid=cats.id ORDER BY expirydate DESC LIMIT $limit";
$db->safe_query($q);
}
}
I've rectified the problem by making the row limit a define, but I'm still baffled - is it a global clash, or something?
Cheers.