[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP mySQL Select query by date

Posted on 2008-11-03
4
Medium Priority
?
312 Views
Last Modified: 2012-05-05
I'm trying to use the following code to select data by a date range and display it.  I can display it fine, but I can't seem to get the mySQL query right...

$url[3]; is an exploded query_string.  The value is 11/01/08

$searchval = $url[3];
        $itemsbydate = mysql_query("SELECT id,group_number,group_name,date_received,disposition,DATE_FORMAT(date_received,'%Y%m%d') as recdate from inquiries where userid='$userid' and recdate=$searchval");
        while($disprow = mysql_fetch_array($itemsbydate, MYSQL_ASSOC))
      {
0
Comment
Question by:SeanStrickland
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22868845
Try like this:
$itemsbydate = mysql_query("SELECT id,group_number,group_name,date_received,disposition,DATE_FORMAT(date_received,'%Y%m%d') as recdate from inquiries where userid='$userid' and date(recdate) = $searchval");

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1200 total points
ID: 22868869
Here is corrected query.  

+Think you want to use date_received in the where clause as recdate is your column alias.
+Use date() to remove any timestamps so everything on 11/01/08 will match regardless of time.
+You may need to have single quotes around date as well.
$itemsbydate = mysql_query("SELECT id,group_number,group_name,date_received,disposition,DATE_FORMAT(date_received,'%Y%m%d') as recdate from inquiries where userid='$userid' and date(date_received) = $searchval");
 
<!-- OR -->
 
$itemsbydate = mysql_query("SELECT id,group_number,group_name,date_received,disposition,DATE_FORMAT(date_received,'%Y%m%d') as recdate from inquiries where userid='$userid' and date(date_received) = '$searchval'");

Open in new window

0
 
LVL 8

Assisted Solution

by:CoyotesIT
CoyotesIT earned 800 total points
ID: 22868886
Try making your $searchval look like "2008-11-01"

and see if you can return the values you are looking for.
0
 
LVL 11

Author Closing Comment

by:SeanStrickland
ID: 31512780
Thanks!  Both of your solutions combined helped me.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question