PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Query
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Query

Réponse
 
LinkBack Outils de la discussion
Vieux 03/11/2007, 02h53   #1
jcage@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Query

I have four fields, "name", "date", "task", and "title" - and am
trying to PHP code where if I click 'All', all rows for 'Bob' followed
by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
would be displayed alphabetically (by name), with all of Bob's rows
grouped together and then all of Jim's and then all of Joe's, etc
(versus the way they may reside in the MySQL table).

My query works well for individual names and also for 'All' but it
would look and be easier to read if I could group the rows by name and
maybe list alphabetically. My 'All' code portion currently looks like
this:

$query =
"SELECT *
FROM $table
WHERE 1 = 1 ";
if($name != "All") $query .= "and name = '".$name."'";

$result = mysql_query($query);


thanks for any ...
John

  Réponse avec citation
Vieux 03/11/2007, 03h12   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

jcage@lycos.com wrote:
> I have four fields, "name", "date", "task", and "title" - and am
> trying to PHP code where if I click 'All', all rows for 'Bob' followed
> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
> would be displayed alphabetically (by name), with all of Bob's rows
> grouped together and then all of Jim's and then all of Joe's, etc
> (versus the way they may reside in the MySQL table).
>
> My query works well for individual names and also for 'All' but it
> would look and be easier to read if I could group the rows by name and
> maybe list alphabetically. My 'All' code portion currently looks like
> this:
>
> $query =
> "SELECT *
> FROM $table
> WHERE 1 = 1 ";
> if($name != "All") $query .= "and name = '".$name."'";
>
> $result = mysql_query($query);
>
>
> thanks for any ...
> John
>
>


You're asking about SQL, not PHP. Try comp.databases.mysql.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 03/11/2007, 04h50   #3
Chris Gorospe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

jcage@lycos.com wrote:
> I have four fields, "name", "date", "task", and "title" - and am
> trying to PHP code where if I click 'All', all rows for 'Bob' followed
> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
> would be displayed alphabetically (by name), with all of Bob's rows
> grouped together and then all of Jim's and then all of Joe's, etc
> (versus the way they may reside in the MySQL table).
>
> My query works well for individual names and also for 'All' but it
> would look and be easier to read if I could group the rows by name and
> maybe list alphabetically. My 'All' code portion currently looks like
> this:
>
> $query =
> "SELECT *
> FROM $table
> WHERE 1 = 1 ";
> if($name != "All") $query .= "and name = '".$name."'";
>
> $result = mysql_query($query);
>
>
> thanks for any ...
> John
>

You're probably going to want to use one of the "joins". 'LEFT JOIN',
'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your
results in alphabetical order.
  Réponse avec citation
Vieux 03/11/2007, 05h39   #4
jcage@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote:
> jc...@lycos.com wrote:
> > I have four fields, "name", "date", "task", and "title" - and am
> > trying to PHP code where if I click 'All', all rows for 'Bob' followed
> > by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
> > would be displayed alphabetically (by name), with all of Bob's rows
> > grouped together and then all of Jim's and then all of Joe's, etc
> > (versus the way they may reside in the MySQL table).

>
> > My query works well for individual names and also for 'All' but it
> > would look and be easier to read if I could group the rows by name and
> > maybe list alphabetically. My 'All' code portion currently looks like
> > this:

>
> > $query =
> > "SELECT *
> > FROM $table
> > WHERE 1 = 1 ";
> > if($name != "All") $query .= "and name = '".$name."'";

>
> > $result = mysql_query($query);

>
> > thanks for any ...
> > John

>
> You're probably going to want to use one of the "joins". 'LEFT JOIN',
> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your
> results in alphabetical order.


Any examples on how to integrate 'join' code for something like this
in PHP? Thanks

  Réponse avec citation
Vieux 03/11/2007, 05h50   #5
Chris Gorospe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

jcage@lycos.com wrote:
> On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote:
>> jc...@lycos.com wrote:
>>> I have four fields, "name", "date", "task", and "title" - and am
>>> trying to PHP code where if I click 'All', all rows for 'Bob' followed
>>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
>>> would be displayed alphabetically (by name), with all of Bob's rows
>>> grouped together and then all of Jim's and then all of Joe's, etc
>>> (versus the way they may reside in the MySQL table).
>>> My query works well for individual names and also for 'All' but it
>>> would look and be easier to read if I could group the rows by name and
>>> maybe list alphabetically. My 'All' code portion currently looks like
>>> this:
>>> $query =
>>> "SELECT *
>>> FROM $table
>>> WHERE 1 = 1 ";
>>> if($name != "All") $query .= "and name = '".$name."'";
>>> $result = mysql_query($query);
>>> thanks for any ...
>>> John

>> You're probably going to want to use one of the "joins". 'LEFT JOIN',
>> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your
>> results in alphabetical order.

>
> Any examples on how to integrate 'join' code for something like this
> in PHP? Thanks
>

  Réponse avec citation
Vieux 03/11/2007, 05h52   #6
Chris Gorospe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

jcage@lycos.com wrote:
> On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote:
>> jc...@lycos.com wrote:
>>> I have four fields, "name", "date", "task", and "title" - and am
>>> trying to PHP code where if I click 'All', all rows for 'Bob' followed
>>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
>>> would be displayed alphabetically (by name), with all of Bob's rows
>>> grouped together and then all of Jim's and then all of Joe's, etc
>>> (versus the way they may reside in the MySQL table).
>>> My query works well for individual names and also for 'All' but it
>>> would look and be easier to read if I could group the rows by name and
>>> maybe list alphabetically. My 'All' code portion currently looks like
>>> this:
>>> $query =
>>> "SELECT *
>>> FROM $table
>>> WHERE 1 = 1 ";
>>> if($name != "All") $query .= "and name = '".$name."'";
>>> $result = mysql_query($query);
>>> thanks for any ...
>>> John

>> You're probably going to want to use one of the "joins". 'LEFT JOIN',
>> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your
>> results in alphabetical order.

>
> Any examples on how to integrate 'join' code for something like this
> in PHP? Thanks
>

Actually, you don't even need to use JOIN. I was under the impression
these people were in different tables.

Just use the 'ORDER BY name DESC' with your 'select *', and that should
get you want you're looking for.
  Réponse avec citation
Vieux 03/11/2007, 12h44   #7
jcage@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

On Nov 2, 9:52 pm, Chris Gorospe <ch...@ekast.com> wrote:
> jc...@lycos.com wrote:
> > On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote:
> >> jc...@lycos.com wrote:
> >>> I have four fields, "name", "date", "task", and "title" - and am
> >>> trying to PHP code where if I click 'All', all rows for 'Bob' followed
> >>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
> >>> would be displayed alphabetically (by name), with all of Bob's rows
> >>> grouped together and then all of Jim's and then all of Joe's, etc
> >>> (versus the way they may reside in the MySQL table).
> >>> My query works well for individual names and also for 'All' but it
> >>> would look and be easier to read if I could group the rows by name and
> >>> maybe list alphabetically. My 'All' code portion currently looks like
> >>> this:
> >>> $query =
> >>> "SELECT *
> >>> FROM $table
> >>> WHERE 1 = 1 ";
> >>> if($name != "All") $query .= "and name = '".$name."'";
> >>> $result = mysql_query($query);
> >>> thanks for any ...
> >>> John
> >> You're probably going to want to use one of the "joins". 'LEFT JOIN',
> >> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your
> >> results in alphabetical order.

>
> > Any examples on how to integrate 'join' code for something like this
> > in PHP? Thanks

>
> Actually, you don't even need to use JOIN. I was under the impression
> these people were in different tables.
>
> Just use the 'ORDER BY name DESC' with your 'select *', and that should
> get you want you're looking for.


Hey, that's it... Thank You Sir... Appreciate the very much. :-)

  Réponse avec citation
Vieux 03/11/2007, 20h29   #8
Tom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

On Fri, 02 Nov 2007 21:12:42 -0500, Jerry Stuckle wrote...
>
>jcage@lycos.com wrote:
>> I have four fields, "name", "date", "task", and "title" - and am
>> trying to PHP code where if I click 'All', all rows for 'Bob' followed
>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
>> would be displayed alphabetically (by name), with all of Bob's rows
>> grouped together and then all of Jim's and then all of Joe's, etc
>> (versus the way they may reside in the MySQL table).
>>
>> My query works well for individual names and also for 'All' but it
>> would look and be easier to read if I could group the rows by name and
>> maybe list alphabetically. My 'All' code portion currently looks like
>> this:
>>
>> $query =
>> "SELECT *
>> FROM $table
>> WHERE 1 = 1 ";
>> if($name != "All") $query .= "and name = '".$name."'";
>>
>> $result = mysql_query($query);
>>
>>
>> thanks for any ...
>> John
>>
>>

>
>You're asking about SQL, not PHP. Try comp.databases.mysql.
>



I get alt.php.sql on my newsgroup list, and that's ful for SQL related
questions too.


Tom
--
NewsGuy Free Trial Accounts
Now a massive 20 Gigabytes of unrestricted downloads !
http://newsguy.com/

  Réponse avec citation
Vieux 04/11/2007, 01h26   #9
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

Tom wrote:
> On Fri, 02 Nov 2007 21:12:42 -0500, Jerry Stuckle wrote...
>> jcage@lycos.com wrote:
>>> I have four fields, "name", "date", "task", and "title" - and am
>>> trying to PHP code where if I click 'All', all rows for 'Bob' followed
>>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe'
>>> would be displayed alphabetically (by name), with all of Bob's rows
>>> grouped together and then all of Jim's and then all of Joe's, etc
>>> (versus the way they may reside in the MySQL table).
>>>
>>> My query works well for individual names and also for 'All' but it
>>> would look and be easier to read if I could group the rows by name and
>>> maybe list alphabetically. My 'All' code portion currently looks like
>>> this:
>>>
>>> $query =
>>> "SELECT *
>>> FROM $table
>>> WHERE 1 = 1 ";
>>> if($name != "All") $query .= "and name = '".$name."'";
>>>
>>> $result = mysql_query($query);
>>>
>>>
>>> thanks for any ...
>>> John
>>>
>>>

>> You're asking about SQL, not PHP. Try comp.databases.mysql.
>>

>
>
> I get alt.php.sql on my newsgroup list, and that's ful for SQL related
> questions too.
>
>
> Tom


But comp.databases.mysql is much better than alt.php.sql for MySQL
questions. The problem is no database implements the SQL standards 100%
or the same way. comp.databases.mysql has MySQL specific info - which,
in this case, is 100% independent of the language being used to access
MySQL, but 100% dependent on MySQL.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 08/11/2007, 06h28   #10
jcage@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

On Nov 2, 8:52 pm, Chris Gorospe <ch...@ekast.com> wrote:

> Just use the 'ORDER BY name DESC' with your 'select *', and that should
> get you want you're looking for.

----------------------------------------

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

So.... :-)

In the above code, 'Order by' works very well when not used in
conjunction with other queryable 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? Any thoughts? TIA


$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);

  Réponse avec citation
Vieux 08/11/2007, 13h25   #11
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

jcage@lycos.com wrote:
> On Nov 2, 8:52 pm, Chris Gorospe <ch...@ekast.com> wrote:
>
>> Just use the 'ORDER BY name DESC' with your 'select *', and that should
>> get you want you're looking for.

> ----------------------------------------
>
> $query = "SELECT DISTINCT names
> FROM $table
> Order by names";
> $result = mysql_query($query);
>
> So.... :-)
>
> In the above code, 'Order by' works very well when not used in
> conjunction with other queryable 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? Any thoughts? TIA
>
>
> $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);
>
>


This isn't a PHP question.

Try comp.database.mysql when asking mysql questions. That's where the
SQL experts hang out.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 08/11/2007, 14h09   #12
jcage@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

On Nov 8, 5:25 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> jc...@lycos.com wrote:
> > On Nov 2, 8:52 pm, Chris Gorospe <ch...@ekast.com> wrote:

>
> >> Just use the 'ORDER BY name DESC' with your 'select *', and that should
> >> get you want you're looking for.

> > ----------------------------------------

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

>
> > So.... :-)

>
> > In the above code, 'Order by' works very well when not used in
> > conjunction with other queryable 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? Any thoughts? TIA

>
> > $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);

>
> This isn't a PHP question.
>
> Try comp.database.mysql when asking mysql questions. That's where the
> SQL experts hang out.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================


will do. thanks for the tip.

  Réponse avec citation
Vieux 08/11/2007, 14h13   #13
jcage@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

$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);


  Réponse avec citation
Vieux 08/11/2007, 15h12   #14
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

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.

  Réponse avec citation
Vieux 08/11/2007, 15h34   #15
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query


"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.


  Réponse avec citation
Vieux 08/11/2007, 19h02   #16
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

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 You won't give up until you educate
all newbies to
proper formatting, eh?

Cheers

  Réponse avec citation
Vieux 08/11/2007, 19h43   #17
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query


"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 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.


  Réponse avec citation
Vieux 08/11/2007, 20h45   #18
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

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!


  Réponse avec citation
Vieux 08/11/2007, 21h40   #19
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query


"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.



  Réponse avec citation
Vieux 08/11/2007, 22h14   #20
strawberry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

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 ;-)

  Réponse avec citation
Vieux 08/11/2007, 22h21   #21
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query


"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.


  Réponse avec citation
Vieux 09/11/2007, 10h13   #22
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query

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.

  Réponse avec citation
Vieux 09/11/2007, 14h08   #23
Steve