I have a mysql query that I would like to execute from within PHP script. However, its not working and I believe its related to the proper usage of PHP variables.
Here is the mysql query that works fine by itself:
Here is the PHP code (minus the connect string stuff which I use all the time elsewhere):
Via the print statements above (when uncommented), I can see that the variables are being correctly created. But no threads are being deleted which the pure mysql code does fine.
It must be something very simple. But with only limited PHP experience, I'm not seeing the problem.
Does anyone know what I'm doing wrong?
Thanks. -- Rik
ps: If you are curious, this is all related to importing Usenet newsgroups hourly.
Here is the mysql query that works fine by itself:
Code:
SET @DaysPastMax = 15; SELECT @Date_Now:=UNIX_TIMESTAMP(); set @SecondsPastMax = @DaysPastMax * 86400; set @Date2UseMax = @Date_Now - @SecondsPastMax; DELETE all_rows.* FROM thread AS all_rows INNER JOIN ( SELECT title, dateline, min(threadid) as min_threadid FROM thread GROUP BY title HAVING count(*) >1 AND dateline BETWEEN @Date2UseMax AND @Date_Now ) AS dupe_rows ON dupe_rows.title = all_rows.title # In other words, exclude the lowest threadid of a dupe group AND dupe_rows.min_threadid < all_rows.threadid;
PHP Code:
<?php
// Finds and deletes duplicate threads (based on highest thread number)
$UnixTime = time();
$DaysBack = 15;
$SecondsBack = $DaysBack * 86400;
$DateBack = $UnixTime - $SecondsBack;
/*
print "---<br />";
print $UnixTime . "<br />";
print $DaysBack . "<br />";
print $SecondsBack . "<br />";
print $DateBack . "<br />";
print "---";
*/
$query1="
DELETE all_rows.* FROM thread AS all_rows
INNER JOIN (
SELECT title, dateline, min(threadid) as min_threadid FROM thread
GROUP BY title
HAVING count(*) >1
AND dateline BETWEEN '$DateBack' AND '$UnixTime'
) AS dupe_rows ON dupe_rows.title = all_rows.title
AND dupe_rows.min_threadid < all_rows.threadid";
mysql_query($query1);
mysql_close();
?>
It must be something very simple. But with only limited PHP experience, I'm not seeing the problem.
Does anyone know what I'm doing wrong?
Thanks. -- Rik
ps: If you are curious, this is all related to importing Usenet newsgroups hourly.
Comment