PowWeb Forums - The Perfect Community for the Perfect Host  

Register now to interact with over 11,000 members! Registered users have Posting Privileges, free access to Private Messaging, Email Notifications and more.

Go Back   PowWeb Community Forums > The PowWeb Platform > MySQL
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Closed Thread
 
Thread Tools Search this Thread
Old 12-22-04, 12:32 PM   #1
samlutley
 
Join Date: Oct 2003
Location: Blacksburg, VA
Posts: 65
Reputation: 5
Using WHERE in MySQL query

If I have two variables:

$start
$end

How would I query entries from a data base with dates between $start and $end

Would it be something like:

SELECT id, date FROM my_db_table WHERE date>=$start && date<=end ORDER BY date ASC

thanks,
sam
samlutley is offline  
Old 12-22-04, 03:16 PM   #2
Pig
foo
 
Pig's Avatar
 
Join Date: Jan 2003
Location: Seattle-ish
Posts: 2,597
Reputation: 106
Correct, except that it would be $end, not end.
__________________
webhead.cc
Pig is offline  
Old 12-22-04, 03:24 PM   #3
det1smc
 
Posts: n/a
expr BETWEEN min AND max

almost...

the and operator in SQL is "AND", not "&&".
It's also a good idea to use identifiers "`" if you have a filed name like date.
Code:
SELECT `id`, `date` FROM my_db_table WHERE `date`>='$start' AND `date`<='$end' ORDER BY 'date' ASC


You can also use the BETWEEN operator. It does the same thing, but it's a little easier to read.
Code:
SELECT `id`, `date` FROM my_db_table WHERE `date` BETWEEN '$start' AND '$end' ORDER BY 'date' ASC
 
Old 12-22-04, 03:29 PM   #4
samlutley
 
Join Date: Oct 2003
Location: Blacksburg, VA
Posts: 65
Reputation: 5
thanks
samlutley is offline  
Old 12-22-04, 03:33 PM   #5
Pig
foo
 
Pig's Avatar
 
Join Date: Jan 2003
Location: Seattle-ish
Posts: 2,597
Reputation: 106
I thought mysql allows &&
http://dev.mysql.com/doc/mysql/en/Lo...Operators.html
__________________
webhead.cc
Pig is offline  
Old 12-22-04, 03:53 PM   #6
det1smc
 
Posts: n/a
Red face

Quote:
Originally Posted by Pig
Indeed, you are correct, and I am not .
I've been using SQL (not mySQL) for many years and I've never seen && used in a WHERE.

Thanks
 
Old 12-22-04, 04:21 PM   #7
samlutley
 
Join Date: Oct 2003
Location: Blacksburg, VA
Posts: 65
Reputation: 5
building on but not directly related to...

If I have a string:

Code:
$output = " rowspan = \$number "

And then calculate the value of $number (let's say $number = 3)

How do I successfully echo/print out:

rowspan=3

using something to the effect of

Code:
echo $output

right now it just prints

rowspan = $number
samlutley is offline  
Old 12-22-04, 05:09 PM   #8
det1smc
 
Posts: n/a
Quote:
Originally Posted by samlutley
$output = " rowspan = \$number "

If you can assign a value to $number before you create the $output variable.
Code:
$number=3; $output = " rowspan =\"$number\""; echo $output; //rowspan ="3"

If the value of $number is unkown, you can use this approach.
Code:
$output = " rowspan =\"%d\""; $number=3; echo sprintf($output,$number); //rowspan ="3"
 
Old 12-22-04, 05:32 PM   #9
Pig
foo
 
Pig's Avatar
 
Join Date: Jan 2003
Location: Seattle-ish
Posts: 2,597
Reputation: 106
mysql is not very strict about complying to standards. That's why you can insert strings into int columns

This is perfectly valid to mysql:
INSERT INTO table (some_int_col) VALUES ('3');
__________________
webhead.cc
Pig is offline  
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 07:30 PM.


Contents ©PowWeb, Inc. ~ vBulletin, Copyright © 2000-2007 Jelsoft Enterprises Limited.