/    Sign up×
Community /Pin to ProfileBookmark

Query::Displaying Only past 7 days and am I using stripslashes right?

[code=php]
<?php
$db_name = “markbad_markbadsql”;
$table_name = “event”;
$connection = mysql_connect (“localhost”, “markbad_drpl1”, “n4x4q37IhCez”)
or die (‘I cannot connect to the database because: ‘ . mysql_error());
$db = mysql_select_db ($db_name, $connection)
or die (mysql_error());
//sort by date.. only monday thru friday
$sql = “SELECT *, curdate() AS c, dayofweek(curdate()) AS d FROM $table_name WHERE date BETWEEN date_sub(c INTERVAL (d -1) DAYS) AND date_add(c INTERVAL (7-c) DAYS)”;
$result = mysql_query($sql,$connection)
or die (mysql_error());
//While loop will create an array called $row
while ($row = mysql_fetch_array($result)) {
// get individual elements from events
$date = stripslashes($row[‘date’]);
$bar = stripslashes($row[‘bar’]);
$updated = stripslashes($row[‘updated’]);
$details = stripslashes($row[‘details’]);
$map_url = stripslashes($row[‘map_url’]);
$display_block .= ”
t<h4>$bar <span class=”date”> $updated</span></h4>
t<p>
tt&details<br />
tt<span class=”date”>$map_url</span>
t</p>”;
}
?>

//bunch of html

<? echo “$display_block”; ?>

[/code]

I get a funky error from mysql

[CODE]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 ‘INTERVAL (d -1) DAYS) AND date_add(c INTERVAL (7-c) DAYS)’ at line 1[/CODE]

Please Help. What I want to do with the query is display all of the events within listed from “ONLY” the past seven days. not any before that

Also am I using stripslashes right?

Thanks!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 10.2006 — Well, for last 7 days, I think you might want something like:
[code=php]
$query = "SELECT * FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
[/code]
Copy linkTweet thisAlerts:
@Markbad311authorJan 10.2006 — so what about the stripslashes? is that correct? One more thing is that syntax for selecting the last 7 days, or past 7 and future 7?
Copy linkTweet thisAlerts:
@NogDogJan 10.2006 — The sample code I provided was intended to provide 7 days of data, with the latest day being today. If you want 7 days up to but not including today, then change it to:
[code=php]
$query = "SELECT * FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) and CURDATE()";
[/code]

I see nothing wrong with the stripslashes() syntax, though I'm not sure there's any reason to use it here.
Copy linkTweet thisAlerts:
@Markbad311authorJan 10.2006 — Ok One more thing other then that I have ran into allready. Some of my data runs off my page. this is probally a bad thing so I don't want to change the page rather, I would like to have it go through the table and display only so many rows at a time then have some kind of variable to trigger the next sets and keep going till the end. I reall don't want it to leave the one php page. Other then that everything works accordingly
Copy linkTweet thisAlerts:
@chazzyJan 10.2006 — you can use the MySQL limit function:

LIMIT 0,10

starting with the 0th row, shows the next 10 rows.

LIMIT 10,10

start with the 10th row, shows the next 10 rows

etc.

you can use a get parameter to determine how many rows to offset in your query.
×

Success!

Help @Markbad311 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...