Solved

Date in MySQL and PHP the best way to accomplish

Posted on 2007-04-04
6
263 Views
Last Modified: 2013-12-12
Hi,

I have this table (number (int), task (varchar 80) date (date)).

What I want is the ability to search for a task in a specific date, or between two dates.

I don't know if the table structure that I have created is correct or not.  Or the best way to accomplish this task?

I tried running this query in PHP

$query = "SELECT task FROM taskmngr WHERE date between '2002-08-06' and '2007-04-30'";

But I got this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\location\all.php on line 14
No data to display!

Here is line 14:

$numrows = mysql_num_rows($result);

Could someone please help me?

Thanks!
0
Comment
Question by:bprof2007
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18851990
table (number (int), task (varchar 80) `date` (date)).


$query = "SELECT task FROM taskmngr WHERE `date` between '2002-08-06' and '2007-04-30'";
0
 
LVL 24

Accepted Solution

by:
glcummins earned 250 total points
ID: 18852007
Add an error reporting piece to your mysql_query line:

$result = mysql_query($query) or die("Failed while performing the following query: $query<br />MySQL error: " . mysql_error());

Note that this line must come after you create your query, and before you try to use mysql_num_rows():

<?php
 $query = "SELECT task FROM taskmngr WHERE date between '2002-08-06' and '2007-04-30'";
 $result = mysql_query($query) or die("Failed while performing the following query: $query<br />MySQL error: " . mysql_error());
 $numrows = mysql_num_rows($result);
?>
0
 
LVL 1

Author Comment

by:bprof2007
ID: 18852155
Thanks angellll, I'm sorry I didn't understand what difference `date` will make.

Thanks glcummins, your statement helped me detect the error, which was table name error (date_ not date)

Can I limit the display result through the query itself? i.e. the results were over 20 tasks, I only want to display 10 tasks-oldest tasks-?

Is there a way to do that? If not, what is the other way to do it?

Thanks for all your help guys.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 1

Author Comment

by:bprof2007
ID: 18852422
The question is now 500 points worth after adding the last question.

Thanks,
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18852461
>Thanks angellll, I'm sorry I didn't understand what difference `date` will make.
date is a reserved keyword, so a column or a table with that names needs to be escaped. in Mysql, that is with backticks `


>Can I limit the display result through the query itself? i.e. the results were over 20 tasks, I only want to display 10 tasks-oldest tasks-?


 $query = "SELECT task FROM taskmngr WHERE date between '2002-08-06' and '2007-04-30' ORDER BY date ASC LIMIT 10";
0
 
LVL 1

Author Comment

by:bprof2007
ID: 18852667
Thanks angellll, worked fine.

Thank you guys a lot for your help!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 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