Re: Query Help

This is a discussion on Re: Query Help within the MySQL Database forums, part of the Database Forums category; $query = "SELECT DISTINCT names FROM $table Order by names"; $result = mysql_query($query); In the code above, 'Order by' ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-08-2007
jcage@lycos.com
 
Posts: n/a
Default Re: Query Help

$query = "SELECT DISTINCT names
FROM $table
Order by names";
$result = mysql_query($query);

In the code above, 'Order by' works very well when not used in
conjunction with other query-able items but how about when used in a
query where other possible choices are included? How do I use 'Order
by' in the query below where it will only act on 'names' when "All"
is
selected and otherwise, not really be active during the query as in
the code below? I'm using PHP to access MySQL. Thanks

$query = "SELECT *
FROM $table
WHERE 1 = 1 ";
if($year != "All") $query .= "and year = '".$year."'";
if($status != "All") $query .= "and status = '".
$status."'";
if($names != "All") $query .= "and names = '".$names."'";
$result = mysql_query($query);


Reply With Quote
  #2 (permalink)  
Old 11-08-2007
Captain Paralytic
 
Posts: n/a
Default Re: Query Help

On 8 Nov, 14:13, jc...@lycos.com wrote:
> $query = "SELECT DISTINCT names
> FROM $table
> Order by names";
> $result = mysql_query($query);
>
> In the code above, 'Order by' works very well when not used in
> conjunction with other query-able items but how about when used in a
> query where other possible choices are included? How do I use 'Order
> by' in the query below where it will only act on 'names' when "All"
> is
> selected and otherwise, not really be active during the query as in
> the code below? I'm using PHP to access MySQL. Thanks
>
> $query = "SELECT *
> FROM $table
> WHERE 1 = 1 ";
> if($year != "All") $query .= "and year = '".$year."'";
> if($status != "All") $query .= "and status = '".
> $status."'";
> if($names != "All") $query .= "and names = '".$names."'";
> $result = mysql_query($query);


ORDER BY names

the whole WHERE clause is superfluous in this query.

Reply With Quote
  #3 (permalink)  
Old 11-08-2007
Steve
 
Posts: n/a
Default Re: Query Help


"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
> On 8 Nov, 14:13, jc...@lycos.com wrote:
>> $query = "SELECT DISTINCT names
>> FROM $table
>> Order by names";
>> $result = mysql_query($query);
>>
>> In the code above, 'Order by' works very well when not used in
>> conjunction with other query-able items but how about when used in a
>> query where other possible choices are included? How do I use 'Order
>> by' in the query below where it will only act on 'names' when "All"
>> is
>> selected and otherwise, not really be active during the query as in
>> the code below? I'm using PHP to access MySQL. Thanks
>>
>> $query = "SELECT *
>> FROM $table
>> WHERE 1 = 1 ";
>> if($year != "All") $query .= "and year = '".$year."'";
>> if($status != "All") $query .= "and status = '".
>> $status."'";
>> if($names != "All") $query .= "and names = '".$names."'";
>> $result = mysql_query($query);

>
> ORDER BY names


just a note about legibility here...

$sql[] = "
SELECT *
FROM " . $table . "
WHERE 1 = 1
";
if ($year != 'ALL')
{
$sql[] = " AND year = '" . $year . "'";
}
if ($status != 'ALL')
{
$sql[] = " AND status = '" . $status. "'";
}
if ($names != 'ALL')
{
$sql[] = " AND names = '" . $names. "'";
}
$sql = implode("\r\n", $sql);

that avoide the problem of forgetting to put a space between the if'd
conditions/criterion. note, i put the spaces before 'and' simply so that
when i go to debug the current sql being run, it echos nice and formatted in
the browser.


Reply With Quote
  #4 (permalink)  
Old 11-08-2007
Darko
 
Posts: n/a
Default Re: Query Help

On Nov 8, 4:34 pm, "Steve" <no....@example.com> wrote:
> "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
>
> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
>
>
>
> > On 8 Nov, 14:13, jc...@lycos.com wrote:
> >> $query = "SELECT DISTINCT names
> >> FROM $table
> >> Order by names";
> >> $result = mysql_query($query);

>
> >> In the code above, 'Order by' works very well when not used in
> >> conjunction with other query-able items but how about when used in a
> >> query where other possible choices are included? How do I use 'Order
> >> by' in the query below where it will only act on 'names' when "All"
> >> is
> >> selected and otherwise, not really be active during the query as in
> >> the code below? I'm using PHP to access MySQL. Thanks

>
> >> $query = "SELECT *
> >> FROM $table
> >> WHERE 1 = 1 ";
> >> if($year != "All") $query .= "and year = '".$year."'";
> >> if($status != "All") $query .= "and status = '".
> >> $status."'";
> >> if($names != "All") $query .= "and names = '".$names."'";
> >> $result = mysql_query($query);

>
> > ORDER BY names

>
> just a note about legibility here...
>
> $sql[] = "
> SELECT *
> FROM " . $table . "
> WHERE 1 = 1
> ";
> if ($year != 'ALL')
> {
> $sql[] = " AND year = '" . $year . "'";}
>
> if ($status != 'ALL')
> {
> $sql[] = " AND status = '" . $status. "'";}
>
> if ($names != 'ALL')
> {
> $sql[] = " AND names = '" . $names. "'";}
>
> $sql = implode("\r\n", $sql);
>
> that avoide the problem of forgetting to put a space between the if'd
> conditions/criterion. note, i put the spaces before 'and' simply so that
> when i go to debug the current sql being run, it echos nice and formatted in
> the browser.


You and your formatting again :D You won't give up until you educate
all newbies to
proper formatting, eh? :)

Cheers

Reply With Quote
  #5 (permalink)  
Old 11-08-2007
Steve
 
Posts: n/a
Default Re: Query Help


"Darko" <darko.maksimovic@gmail.com> wrote in message
news:1194548559.891114.33150@v23g2000prn.googlegro ups.com...
> On Nov 8, 4:34 pm, "Steve" <no....@example.com> wrote:
>> "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
>>
>> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
>>
>>
>>
>> > On 8 Nov, 14:13, jc...@lycos.com wrote:
>> >> $query = "SELECT DISTINCT names
>> >> FROM $table
>> >> Order by names";
>> >> $result = mysql_query($query);

>>
>> >> In the code above, 'Order by' works very well when not used in
>> >> conjunction with other query-able items but how about when used in a
>> >> query where other possible choices are included? How do I use 'Order
>> >> by' in the query below where it will only act on 'names' when "All"
>> >> is
>> >> selected and otherwise, not really be active during the query as in
>> >> the code below? I'm using PHP to access MySQL. Thanks

>>
>> >> $query = "SELECT *
>> >> FROM $table
>> >> WHERE 1 = 1 ";
>> >> if($year != "All") $query .= "and year = '".$year."'";
>> >> if($status != "All") $query .= "and status = '".
>> >> $status."'";
>> >> if($names != "All") $query .= "and names = '".$names."'";
>> >> $result = mysql_query($query);

>>
>> > ORDER BY names

>>
>> just a note about legibility here...
>>
>> $sql[] = "
>> SELECT *
>> FROM " . $table . "
>> WHERE 1 = 1
>> ";
>> if ($year != 'ALL')
>> {
>> $sql[] = " AND year = '" . $year . "'";}
>>
>> if ($status != 'ALL')
>> {
>> $sql[] = " AND status = '" . $status. "'";}
>>
>> if ($names != 'ALL')
>> {
>> $sql[] = " AND names = '" . $names. "'";}
>>
>> $sql = implode("\r\n", $sql);
>>
>> that avoide the problem of forgetting to put a space between the if'd
>> conditions/criterion. note, i put the spaces before 'and' simply so that
>> when i go to debug the current sql being run, it echos nice and formatted
>> in
>> the browser.

>
> You and your formatting again :D You won't give up until you educate
> all newbies to
> proper formatting, eh? :)


he, he, he, he!

well, actually the formatting is just a bonus. some db's, even though they
strip out spaces themselves when parsing, will caugh up errors if the
clauses run together...which happens the way the op wrote it...where 1=1and
year='2007'and status=... building $sql as an array and then imploding it
back on itself avoids the problem and also makes for more clearly defined if
() cases.

but, i'm still chuckling. that was good.


Reply With Quote
  #6 (permalink)  
Old 11-08-2007
Paul Lautman
 
Posts: n/a
Default Re: Query Help

Steve wrote:
> "Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
>> On 8 Nov, 14:13, jc...@lycos.com wrote:
>>> $query = "SELECT DISTINCT names
>>> FROM $table
>>> Order by names";
>>> $result = mysql_query($query);
>>>
>>> In the code above, 'Order by' works very well when not used in
>>> conjunction with other query-able items but how about when used in
>>> a query where other possible choices are included? How do I use
>>> 'Order by' in the query below where it will only act on 'names'
>>> when "All" is
>>> selected and otherwise, not really be active during the query as in
>>> the code below? I'm using PHP to access MySQL. Thanks
>>>
>>> $query = "SELECT *
>>> FROM $table
>>> WHERE 1 = 1 ";
>>> if($year != "All") $query .= "and year = '".$year."'";
>>> if($status != "All") $query .= "and status = '".
>>> $status."'";
>>> if($names != "All") $query .= "and names = '".$names."'";
>>> $result = mysql_query($query);

>>
>> ORDER BY names

>
> just a note about legibility here...
>
> $sql[] = "
> SELECT *
> FROM " . $table . "
> WHERE 1 = 1
> ";
> if ($year != 'ALL')
> {
> $sql[] = " AND year = '" . $year . "'";
> }
> if ($status != 'ALL')
> {
> $sql[] = " AND status = '" . $status. "'";
> }
> if ($names != 'ALL')
> {
> $sql[] = " AND names = '" . $names. "'";
> }
> $sql = implode("\r\n", $sql);
>
> that avoide the problem of forgetting to put a space between the if'd
> conditions/criterion. note, i put the spaces before 'and' simply so
> that when i go to debug the current sql being run, it echos nice and
> formatted in the browser.


Please learn to post replies to the correct post. I do not need advise on
formatting, the OP may do!


Reply With Quote
  #7 (permalink)  
Old 11-08-2007
Steve
 
Posts: n/a
Default Re: Query Help


"Paul Lautman" <paul.lautman@btinternet.com> wrote in message
news:5phaqhFrfrejU1@mid.individual.net...
> Steve wrote:
>> "Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
>> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com...
>>> On 8 Nov, 14:13, jc...@lycos.com wrote:
>>>> $query = "SELECT DISTINCT names
>>>> FROM $table
>>>> Order by names";
>>>> $result = mysql_query($query);
>>>>
>>>> In the code above, 'Order by' works very well when not used in
>>>> conjunction with other query-able items but how about when used in
>>>> a query where other possible choices are included? How do I use
>>>> 'Order by' in the query below where it will only act on 'names'
>>>> when "All" is
>>>> selected and otherwise, not really be active during the query as in
>>>> the code below? I'm using PHP to access MySQL. Thanks
>>>>
>>>> $query = "SELECT *
>>>> FROM $table
>>>> WHERE 1 = 1 ";
>>>> if($year != "All") $query .= "and year = '".$year."'";
>>>> if($status != "All") $query .= "and status = '".
>>>> $status."'";
>>>> if($names != "All") $query .= "and names = '".$names."'";
>>>> $result = mysql_query($query);
>>>
>>> ORDER BY names

>>
>> just a note about legibility here...
>>
>> $sql[] = "
>> SELECT *
>> FROM " . $table . "
>> WHERE 1 = 1
>> ";
>> if ($year != 'ALL')
>> {
>> $sql[] = " AND year = '" . $year . "'";
>> }
>> if ($status != 'ALL')
>> {
>> $sql[] = " AND status = '" . $status. "'";
>> }
>> if ($names != 'ALL')
>> {
>> $sql[] = " AND names = '" . $names. "'";
>> }
>> $sql = implode("\r\n", $sql);
>>
>> that avoide the problem of forgetting to put a space between the if'd
>> conditions/criterion. note, i put the spaces before 'and' simply so
>> that when i go to debug the current sql being run, it echos nice and
>> formatted in the browser.

>
> Please learn to post replies to the correct post. I do not need advise on
> formatting, the OP may do!


first of all, it's a thread, dude. second, it's usenet. like it, or lump it.
:)


Reply With Quote
  #8 (permalink)  
Old 11-08-2007
strawberry
 
Posts: n/a
Default Re: Query Help

On 8 Nov, 20:45, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
> Steve wrote:
> > "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
> >news:1194534736.193031.304830@q5g2000prf.googlegr oups.com...
> >> On 8 Nov, 14:13, jc...@lycos.com wrote:
> >>> $query = "SELECT DISTINCT names
> >>> FROM $table
> >>> Order by names";
> >>> $result = mysql_query($query);

>
> >>> In the code above, 'Order by' works very well when not used in
> >>> conjunction with other query-able items but how about when used in
> >>> a query where other possible choices are included? How do I use
> >>> 'Order by' in the query below where it will only act on 'names'
> >>> when "All" is
> >>> selected and otherwise, not really be active during the query as in
> >>> the code below? I'm using PHP to access MySQL. Thanks

>
> >>> $query = "SELECT *
> >>> FROM $table
> >>> WHERE 1 = 1 ";
> >>> if($year != "All") $query .= "and year = '".$year."'";
> >>> if($status != "All") $query .= "and status = '".
> >>> $status."'";
> >>> if($names != "All") $query .= "and names = '".$names."'";
> >>> $result = mysql_query($query);

>
> >> ORDER BY names

>
> > just a note about legibility here...

>
> > $sql[] = "
> > SELECT *
> > FROM " . $table . "
> > WHERE 1 = 1
> > ";
> > if ($year != 'ALL')
> > {
> > $sql[] = " AND year = '" . $year . "'";
> > }
> > if ($status != 'ALL')
> > {
> > $sql[] = " AND status = '" . $status. "'";
> > }
> > if ($names != 'ALL')
> > {
> > $sql[] = " AND names = '" . $names. "'";
> > }
> > $sql = implode("\r\n", $sql);

>
> > that avoide the problem of forgetting to put a space between the if'd
> > conditions/criterion. note, i put the spaces before 'and' simply so
> > that when i go to debug the current sql being run, it echos nice and
> > formatted in the browser.

>
> Please learn to post replies to the correct post. I do not need advise on
> formatting, the OP may do!


Definitely not, but some advice on spelling (and punctuation) wouldn't
go amiss. He he he ;-)

Reply With Quote
  #9 (permalink)  
Old 11-08-2007
Steve
 
Posts: n/a
Default Re: Query Help


"strawberry" <zac.carey@gmail.com> wrote in message
news:1194560072.097968.186940@s15g2000prm.googlegr oups.com...
> On 8 Nov, 20:45, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
>> Steve wrote:
>> > "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
>> >news:1194534736.193031.304830@q5g2000prf.googlegr oups.com...
>> >> On 8 Nov, 14:13, jc...@lycos.com wrote:
>> >>> $query = "SELECT DISTINCT names
>> >>> FROM $table
>> >>> Order by names";
>> >>> $result = mysql_query($query);

>>
>> >>> In the code above, 'Order by' works very well when not used in
>> >>> conjunction with other query-able items but how about when used in
>> >>> a query where other possible choices are included? How do I use
>> >>> 'Order by' in the query below where it will only act on 'names'
>> >>> when "All" is
>> >>> selected and otherwise, not really be active during the query as in
>> >>> the code below? I'm using PHP to access MySQL. Thanks

>>
>> >>> $query = "SELECT *
>> >>> FROM $table
>> >>> WHERE 1 = 1 ";
>> >>> if($year != "All") $query .= "and year = '".$year."'";
>> >>> if($status != "All") $query .= "and status = '".
>> >>> $status."'";
>> >>> if($names != "All") $query .= "and names = '".$names."'";
>> >>> $result = mysql_query($query);

>>
>> >> ORDER BY names

>>
>> > just a note about legibility here...

>>
>> > $sql[] = "
>> > SELECT *
>> > FROM " . $table . "
>> > WHERE 1 = 1
>> > ";
>> > if ($year != 'ALL')
>> > {
>> > $sql[] = " AND year = '" . $year . "'";
>> > }
>> > if ($status != 'ALL')
>> > {
>> > $sql[] = " AND status = '" . $status. "'";
>> > }
>> > if ($names != 'ALL')
>> > {
>> > $sql[] = " AND names = '" . $names. "'";
>> > }
>> > $sql = implode("\r\n", $sql);

>>
>> > that avoide the problem of forgetting to put a space between the if'd
>> > conditions/criterion. note, i put the spaces before 'and' simply so
>> > that when i go to debug the current sql being run, it echos nice and
>> > formatted in the browser.

>>
>> Please learn to post replies to the correct post. I do not need advise on
>> formatting, the OP may do!

>
> Definitely not, but some advice on spelling (and punctuation) wouldn't
> go amiss. He he he ;-)


sorry, if you look at my posts, they're splintered with errors on all
counts. :) hell, i even leave out words altogether sometimes...damn fingers
just can't keep up.


Reply With Quote
  #10 (permalink)  
Old 11-09-2007
Captain Paralytic
 
Posts: n/a
Default Re: Query Help

On 8 Nov, 21:40, "Steve" <no....@example.com> wrote:
| first of all, it's a thread, dude.
Err no. A thread is a linked collection of posts on a particular
subject. When you post a reply you do so to a previous post within a
thread, dude.

| second, it's usenet. like it, or lump it.
It is not usenet's fault. Usenet is quite capable of attaching
responses correctly. What is needed is a bit of intelligence on the
part of the user, dude.

Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:04 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0