PDA

View Full Version : SQL Query based on last 48 hours?


PhilMcKrackon
05-04-2008, 09:38 PM
I have a PHP file that I run as a cron task every 24 hours. Can some one help with a tweak to the query to only select matches that have occurred between the time the query is executed and through 48 hours before?

$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC");
while ($urlrow = mysql_fetch_array($urls)) {
fwrite($file," ");
fwrite($file, $urlrow[1]);
fwrite($file, " | ");
fwrite($file," ");
fwrite($file, $urlrow[0]);
fwrite($file," \n");
}

How would I change $urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC"); to accomplish this?

Any help would be appreciated!

Thanks,

MoT3rror
05-04-2008, 11:18 PM
Is there a time column in your table?

PhilMcKrackon
05-04-2008, 11:27 PM
Yes there is - its "dateline" and as an example under PHPMyAdmin the field looks like "1207498258" for a particular entry. I am unsure how to read that with the SQL query and translate that to what I need.

Thanks for the reply,

MoT3rror
05-05-2008, 12:08 AM
$time = mktime(date('H'), date('i'), date('s'), date("m"), date('d')-2, date('Y'));

$data = $db->query_read("SELECT * FROM table WHERE timecolumn > '". date('Y-m-d H:i:s', $time) . "'");

PhilMcKrackon
05-05-2008, 12:26 AM
Thank you very much for the help. I'll give it a try.

Eikinskjaldi
05-05-2008, 12:51 AM
More easily done within mysql

...where unix_timestamp() - timecolumn <= 3600*48

PhilMcKrackon
05-05-2008, 01:46 PM
After many frustrating hours I'm still having a problem. I have tried multiple ways of writing the SQL query line but it still is not correct. I am using this code $time = mktime(date('H'), date('i'), date('s'), date("m"), date('d')-2, date('Y'));
$urls = mysql_query("SELECT linkid,linkurl FROM adv_links WHERE dateline > '". date('Y-m-d H:i:s', $time) . "' ORDER by linkid ASC");But my query is still returning all results from the beginning, not just the last two days. The query does sort by the correct order though. Any help would again be appreciated.

Thanks,

Marco van Herwaarden
05-06-2008, 10:43 AM
Try:$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC WHERE dateline > " . (TIMENOW - (2 * 24 * 60 * 60)));

PhilMcKrackon
05-06-2008, 02:06 PM
Try:$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC WHERE dateline > " . (TIMENOW - (2 * 24 * 60 * 60)));Thanks for the help but that bit of code gives me the error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*****/public_html/pagevisD.php on line 19 on the line after the SQL query call. I edited the SQL cal that works and changed linkid,linkurl to linkid,linkurl,dateline - then added fwrite($file, $urlrow[2]); to the output file as a test (and the date shows up) so I know it's not the dateline table entry that is the problem.

Other thoughts?

--------------- Added 1210086427 at 1210086427 ---------------

More easily done within mysql

...where unix_timestamp() - timecolumn <= 3600*48Sorry I missed this post, could you elaborate?

Thanks,

--------------- Added 1210108992 at 1210108992 ---------------

$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC WHERE unix_timestamp() - timecolumn <= 3600*48");

Eikinskjaldi, tried this as the query and I get the same PHP error as above - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*****/public_html/pagevisD.php on line 19 I have tried several variations of both syntaxes above and the error persists. I'll keep trying...

Eikinskjaldi
05-07-2008, 03:33 AM
"timecolumn" is whatever field in your database contains the dateline data

its kicking an error because you dont actually have a field called "timecolumn"


It looks like your field is called dateline, so the sql is


SELECT linkid,linkurl FROM adv_links
ORDER by linkid ASC
WHERE unix_timestamp() - dateline <= 3600*48

Marco van Herwaarden
05-07-2008, 08:21 AM
Thanks for the help but that bit of code gives me the error
Please post the entire part of the code where this query is executed and the rows are read.

PhilMcKrackon
05-07-2008, 09:48 AM
"timecolumn" is whatever field in your database contains the dateline data

its kicking an error because you dont actually have a field called "timecolumn"


It looks like your field is called dateline, so the sql is


SELECT linkid,linkurl FROM adv_links
ORDER by linkid ASC
WHERE unix_timestamp() - dateline <= 3600*48
That was my fault, I posted the code before I had edited it. Here is the code that I used and I still get the invalid MYSQL result resource in the fetch_array.

//$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC");
$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC WHERE unix_timestamp() - dateline <= 3600*48");
while ($urlrow = mysql_fetch_array($urls)) {
fwrite($file," ");
fwrite($file, $urlrow[1]);
fwrite($file, " | ");
fwrite($file," ");
fwrite($file, $urlrow[0]);
fwrite($file," \n");
}
fclose($file);
?>

And the error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*****/public_html/pagevisD.php on line 13

I'm wondering if I have the edquotes in the wrong postion? The commented '//' out line is the original query line that works. I made sure that there was new data in the table during the last 48 hours so that the query should always yield a result. If I add 'dateline' to the query and add a fwrite for $urlrow[2] the date in the table is written to the file so dateline is the correct field.

Please post the entire part of the code where this query is executed and the rows are read.
Marco, I used the same code above with the SQL query you supplied.

Thanks,

Marco van Herwaarden
05-07-2008, 10:00 AM
$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC WHERE dateline > " . (TIMENOW - (2 * 24 * 60 * 60)));
while ($urlrow = mysql_fetch_array($urls)) {
fwrite($file," ");
fwrite($file, $urlrow[1]);
fwrite($file, " | ");
fwrite($file," ");
fwrite($file, $urlrow[0]);
fwrite($file," \n");
}
fclose($file);
?>

Should work, providing that you have a valid MySQL database connection. (maybe post the entire script).

PhilMcKrackon
05-07-2008, 10:18 AM
Thanks again but I still get the same error. I'm sure the DB connection is good as I can use the original query with no problems. I'm going to post the entire code also. Sorry to be of so much trouble.Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*****/public_html/pagevisE.php on line 13<?php
#!/usr/lib/php
// Creates Text Input for PageVisualize
$host = "localhost";
$mysql_user = "*****";
$mysql_password = "*****";
$sdatabase = "******";
$slink = mysql_connect("$host", "$mysql_user", "$mysql_password") or die('Database Connection Failed. Wait a moment.');
mysql_select_db ("$sdatabase", $slink);
$file = fopen("TestPhpD.txt", "w");
//$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC");
$urls = mysql_query("SELECT linkid,linkurl FROM adv_links ORDER by linkid ASC WHERE dateline > " . (TIMENOW - (2 * 24 * 60 * 60)));
while ($urlrow = mysql_fetch_array($urls)) {
fwrite($file," ");
fwrite($file, $urlrow[1]);
fwrite($file, " | ");
fwrite($file," ");
fwrite($file, $urlrow[0]);
fwrite($file," \n");
}
fclose($file);
?>

Marco van Herwaarden
05-07-2008, 10:48 AM
Lol, mistake (in all these examples!!) you get by editing code and not read good what the result is.

The following is valid:
$urls = mysql_query("SELECT linkid,linkurl FROM adv_links WHERE dateline > " . (time() - (2 * 24 * 60 * 60)) . " ORDER by linkid ASC");

- ORDER must be after the WHERE clause (can't believe everyone, including myself, made this same mistake)
- TIMENOW is only defined within a vBulletin environment, so replace by time()

PhilMcKrackon
05-07-2008, 10:57 AM
Sucess! I was not aware that ORDER must always follow WHERE. I thank you ALL very much!

Regards,