Link to home
Start Free TrialLog in
Avatar of digarati
digarati

asked on

PHP to display most recent entries using time stamp less than 10 days

Someone lend me some knowledge on this statement i am trying to display the most recent entries less than or equal to 10 days

$sql = "SELECT id, businessname, primarycontact, primaryphoneareacode, primaryphoneprefix, primaryphonenumber, primarycontactemail from vendor WHERE stamp is less than or equal to currenttimestamp -10 days ORDER BY stamp ASC";

here is my format:

2008-06-05 09:15:18
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of digarati
digarati

ASKER

error:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/h/a/l/site/html/beta/home.php on line 34

line 34:

$sql = "SELECT id, businessname, primarycontact, primaryphoneareacode, primaryphoneprefix, primaryphonenumber, primarycontactemail FROM vendor WHERE stamp < DATE_ADD(NOW(),INTERVAL -10 DAYS) ORDER BY stamp ASC";
keep in mind my format
Do you supply a link_id to your mysql_query?

Like mysql_query($sql, $linkId);

The error indicates that the second argument is incorrect. Did your mysql_connect and mysql_select_db go without errors?
Here is a snap of what I have:


if (mysql_errno()) {
      printf("Connect failed: %s\n", mysql_errno());
      exit();
} else {
      $sql = "SELECT id, businessname, primarycontact, primaryphoneareacode, primaryphoneprefix, primaryphonenumber, primarycontactemail FROM vendor WHERE stamp < DATE_ADD(NOW(),INTERVAL -10 DAYS) ORDER BY stamp ASC";
      $res = mysql_query($sql, $mysql);
	  
 if (($res)>1) {
						while ($newArray = mysql_fetch_array($res, MYSQL_ASSOC)) {
                                                $link2="<a href=\"vendor_details.php?id={$newArray['id']}\">$detailsimage</a>";
						$businessname = $newArray['businessname'];
						$primarycontact = $newArray['primarycontact'];
						$primarycontactemail = $newArray['primarycontactemail'];
						$primaryphoneareacode = $newArray['primaryphoneareacode'];
						$primaryphoneprefix = $newArray['primaryphoneprefix'];
						$primaryphonenumber = $newArray['primaryphonenumber'];
						$stamp = $newArray['stamp'];

Open in new window

The error indicates that your $mysql variable is not a valid mysql link. I find it strange though that your code doesn't act on the mysql_errno() clause.

Can you try (for debugging purposes) an on die(mysql_error()) after your mysql_connect and your mysql_select_db() ?

$mysql = mysql_connect($host, $user, $pass) or die(mysql_error()));
mysql_select_db($db, $mysql) or die(mysql_error());

Alternatively you could use
if (mysql_errno()) {
      printf("Connect failed: %s\n", mysql_errno());
      exit();
} else if(!is_link($mysql)) {
      printf("Connect failed for an unknown reason");
      exit();
} else {
did not work: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DAYS) ORDER BY stamp ASC' at line 1 please contact technical support
sorry i left off the database connection earlier I currently have this error:

did not work: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DAYS) ORDER BY stamp ASC' at line 1 please contact technical support
Sorry, try and change DAYS into DAY.
when i switch it to 2 days it still gives me entries beyond the cutoff?
Increasing points for someone who can help me out here
What is the type of stamp field? Timestamp or datetime?
time stamp
I ran the query locally and didn't find any error in it. I don't know what else to tell you when no errors occur.
had to switch this > to this <
thank you