Solved

Date in MySQL and PHP the best way to accomplish

Posted on 2007-04-04
6
253 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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

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

Thanks,
0
 
LVL 142

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now