Kun je ook min en max aanduiden in MySQL?

Pagina: 1
Acties:

  • decramy
  • Registratie: December 2001
  • Laatst online: 13-09 12:47

decramy

root@birdie:~#

Topicstarter
Ik wou eerst min en max defineren in m`n MySQL query.
Toen hoorde dat ik beter moest laten kiezen tussen 2 getallen.

Dus dan zou t zo worden:
code:
1
2
3
4
5
6
7
8
9
10
11
12
    SELECT
        *
    FROM
        motoren
     WHERE
         merk='$merk' AND
         cat='occasions' AND
         type LIKE '$type' AND
        prijs BETWEEN '$prijs1' AND '$prijs2' AND
        km BETWEEN '$km1' AND '$km2' AND
        bouwjaar BETWEEN 'bouwjaar1' AND 'bouwjaar2'    ORDER BY
        '$order' ASC

Maar dan krijg ik altijd nix geselecteerd.
Hoe doe ik dat met die BETWEEN tag`s??

20*375Wp met Enphase IQ7+ micro's | Stiebel Eltron HGE Water/Water WP 9kW | Tesla M3, powered by SmartEVSE | Servertje @ www.coloclue.net


Verwijderd

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    SELECT
        *
    FROM
        motoren
     WHERE
        merk='$merk' AND
        cat='occasions' AND
        type LIKE '$type' AND # <- ?????? like???? waarom???
        prijs BETWEEN ('$prijs1' AND '$prijs2')
        AND
        km BETWEEN ('$km1' AND '$km2')
        AND
        bouwjaar BETWEEN ('$bouwjaar1' AND '$bouwjaar2')
        ORDER BY
        '$order' ASC

je had bouwjaar ipv $bouwjaar, is dat misschien ook niet verkeerd in je code?

  • decramy
  • Registratie: December 2001
  • Laatst online: 13-09 12:47

decramy

root@birdie:~#

Topicstarter
LIKE omdat hij dan alles wat op $type lijkt, ook selecteerd :)

20*375Wp met Enphase IQ7+ micro's | Stiebel Eltron HGE Water/Water WP 9kW | Tesla M3, powered by SmartEVSE | Servertje @ www.coloclue.net


  • Onno
  • Registratie: Juni 1999
  • Niet online
Op zondag 09 december 2001 16:38 schreef decramy het volgende:
LIKE omdat hij dan alles wat op $type lijkt, ook selecteert :)
Uhm.. met LIKE kun je wildcards gebruiken (% en _), hij gaat niet op zoek naar dingen die 'lijken' op wat je hebt ingevuld ofzo hoor. :)

Verwijderd

Op zondag 09 december 2001 16:38 schreef decramy het volgende:
LIKE omdat hij dan alles wat op $type lijkt, ook selecteerd :)
jah alleen heeft het hier weinig nut denk ik...
http://www.mysql.com/doc/S/t/String_comparison_functions.html

  • decramy
  • Registratie: December 2001
  • Laatst online: 13-09 12:47

decramy

root@birdie:~#

Topicstarter
Warning: Supplied argument is not a valid MySQL result resource in /usr/local/home/httpd/vhosts/nubo.nl/httpdocs/occ/search.php on line 186

Als
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php
    $result = mysql_query("
    SELECT
        *
    FROM
        motoren
    WHERE
        merk='$merk' AND
        cat='occasions' AND
        type LIKE '$type' AND
        prijs BETWEEN ('$prijs1' AND '$prijs2') AND
        km BETWEEN ('$km1' AND '$km2') AND
        bouwjaar BETWEEN ('$bouwjaar1' AND '$bouwjaar2')
    ORDER BY
        '$order' ASC
    ",$db);
    while ($myrow = mysql_fetch_array($result)) {
  ?>

En idd. die bouwjaar was ik vergeten... |:(

20*375Wp met Enphase IQ7+ micro's | Stiebel Eltron HGE Water/Water WP 9kW | Tesla M3, powered by SmartEVSE | Servertje @ www.coloclue.net


  • Onno
  • Registratie: Juni 1999
  • Niet online
Als je met haakjes wilt gaan werken, moet dat zo:
code:
1
2
3
4
5
WHERE
  ... AND
  (prijs BETWEEN '$prijs1' AND '$prijs2') AND
  (km BETWEEN '$km1' AND '$km2') AND
  (bouwjaar BETWEEN '$bouwjaar1' AND '$bouwjaar2')

  • decramy
  • Registratie: December 2001
  • Laatst online: 13-09 12:47

decramy

root@birdie:~#

Topicstarter
OK, laat k t hele scipt eens posten:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?
<form method="post" action="secure.php">
  <p align="center"><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="4">Zoek 
    een Motor:</font></b></font></p>
  <table width="50%" border="0">
    <tr> 
    <td width="19%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Merk:</font></td>
    <td colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Type:</font></td>
    </tr>
    <tr> 
    <td width="19%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="merk">
        <?php
            $result = mysql_query("
                      SELECT DISTINCT 
                        merk
                    FROM
                        motoren
                    ORDER BY 
                        merk ASC
                ",$db);
            while ($myrow = mysql_fetch_array($result)) {?>
        <option value="<?php echo $myrow[merk] ?>"> 
        <?php echo $myrow["merk"] ?>
        </option>
        <?php
              }
        ?>
      </select>
      </font></td>
    <td colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <input type="text" name="type">
      </font></td>
    </tr>
    <tr> 
    <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Prijs:</font></td>
    <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Sorteren 
      op:</font></td>
    </tr>
    <tr> 
    <td width="19%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="prijs1">
        <option value="0" selected>&amp;euro;0</option>
        <option value="5000">&amp;euro;5.000</option>
        <option value="10000">&amp;euro;10.000</option>
        <option value="15000">&amp;euro;15.000</option>
        <option value="20000">&amp;euro;20.000</option>
        <option value="25000">&amp;euro;25.000</option>
      </select>
      &amp;nbsp;&amp;nbsp;&amp;nbsp;-</font></td>
    <td width="31%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="prijs2">
        <option value="0">&amp;euro;0</option>
        <option value="5000">&amp;euro;5.000</option>
        <option value="10000">&amp;euro;10.000</option>
        <option value="15000">&amp;euro;15.000</option>
        <option value="20000">&amp;euro;20.000</option>
        <option value="25000">&amp;euro;25.000</option>
        <option value="" selected>...en meer</option>
      </select>
      </font></td>
    <td colspan="2"> <font size="2">
      <select name="order">
        <option value="merk" selected>Merk</option>
        <option value="type">Type</option>
        <option value="prijs">Prijs</option>
        <option value="km">Kilometerstand</option>
        <option value="bouwjaar">Bouwjaar</option>
        <option value="kenteken">Kenteken</option>
      </select>
      </font></td>
    </tr>
    <tr> 
    <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Kilometerstand:</font></td>
    <td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Bouwjaar:</font></td>
    </tr>
    <tr> 
    <td width="19%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="km1">
        <option value="0" selected>0</option>
        <option value="25000">25.000</option>
        <option value="50000">50.000</option>
        <option value="75000">75.000</option>
        <option value="100.000">100.000</option>
        <option value="125000">125.000</option>
      </select>
      &amp;nbsp;&amp;nbsp;&amp;nbsp;-</font></td>
    <td width="31%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="km2">
        <option value="0" selected>0</option>
        <option value="5000">5.000</option>
        <option value="10000">10.000</option>
        <option value="15000">15.000</option>
        <option value="20000">20.000</option>
        <option value="25000">25.000</option>
        <option value="" selected>...en meer</option>
      </select>
      </font></td>
    <td width="22%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="bouwjaar1">
        <option value="1980" selected>1980</option>
        <option value="1981">1981</option>
        <option value="1982">1982</option>
        <option value="1983">1983</option>
        <option value="1984">1984</option>
        <option value="1985">1985</option>
        <option value="1986">1986</option>
        <option value="1987">1987</option>
        <option value="1988">1988</option>
        <option value="1989">1989</option>
        <option value="1990">1990</option>
        <option value="1991">1991</option>
        <option value="1992">1992</option>
        <option value="1993">1993</option>
        <option value="1994">1994</option>
        <option value="1995">1995</option>
        <option value="1996">1996</option>
        <option value="1997">1997</option>
        <option value="1998">1998</option>
        <option value="1999">1999</option>
        <option value="2000">2000</option>
        <option value="2001">2001</option>
        <option value="2002">2002</option>
      </select>
      </font></td>
    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
      <select name="bouwjaar2">
        <option value="1980">1980</option>
        <option value="1981">1981</option>
        <option value="1982">1982</option>
        <option value="1983">1983</option>
        <option value="1984">1984</option>
        <option value="1985">1985</option>
        <option value="1986">1986</option>
        <option value="1987">1987</option>
        <option value="1988">1988</option>
        <option value="1989">1989</option>
        <option value="1990">1990</option>
        <option value="1991">1991</option>
        <option value="1992">1992</option>
        <option value="1993">1993</option>
        <option value="1994">1994</option>
        <option value="1995">1995</option>
        <option value="1996">1996</option>
        <option value="1997">1997</option>
        <option value="1998">1998</option>
        <option value="1999">1999</option>
        <option value="2000">2000</option>
        <option value="2001">2001</option>
        <option value="2002" selected>2002</option>
      </select>
      </font></td>
    </tr>
    <tr> 
    <td colspan="4"> 
      <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
        <input type="hidden" name="action" value="search">
        <input type="hidden" name="search_action" value="groot_ingevuld">
        <input type="submit" name="Submit" value="Zoek motor!">
        </font></div>
    </td>
    </tr>
  </table>
</form>
?>

Is het form
En dan nu de uitwerking
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?
  $query = "
    SELECT
        *
    FROM
        motoren
    WHERE
        (merk='$merk') AND
        (cat='occasions') AND
        (type LIKE '$type') AND
        (prijs BETWEEN '$prijs1' AND '$prijs2') AND
        (km BETWEEN '$km1' AND '$km2') AND
        (bouwjaar BETWEEN '$bouwjaar1' AND '$bouwjaar2')
    ORDER BY
        '$order' ASC
    ";
  $result = mysql_query($query);
  $rowcount = mysql_num_rows($result);
?>
</font>
<center>
  <font face="Verdana, Arial, Helvetica, sans-serif" size="4"><b><BR>
  <BR>
  <BR>
  <?php echo $rowcount; ?>
  item(s) gevonden!</font></b> </font>
</center>
<font size="2">
<?
  if ($rowcount == "0") {
    ?>
</font>
<center>
  <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Niks gevonden met 
  deze zoekopdracht. <a href="http://www.nubo.nl/occ/secure.php?action=search&amp;search_action=groot_invullen">Probeer 
  het nog eens!</a></font> 
</center>
<font size="2">
<?php
  } else {
    ?>
</font>
<table width="200" border="0">
  <tr> 
    <td bgcolor="#0054A6" width="50"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Foto</font></td>
    <td bgcolor="#0054A6" width="5%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Merk</font></td>
    <td bgcolor="#0054A6" width="5%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Type</font></td>
    <td bgcolor="#0054A6" width="9%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Bouwjaar</font></td>
    <td bgcolor="#0054A6" width="14%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Kilometerstand</font></td>
    <td bgcolor="#0054A6" width="5%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Kleur</font></td>
    <td bgcolor="#0054A6" width="15%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Bijzonderheden</font></td>
    <td bgcolor="#0054A6" width="4%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Prijs</font></td>
    <td bgcolor="#0054A6" width="8%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Garantie</font></td>
    <td bgcolor="#0054A6" width="9%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Kenteken</font></td>
    <td bgcolor="#0054A6" width="8%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Wijzigen</font></td>
    <td bgcolor="#0054A6" width="13%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Verwijderen</font></td>
  </tr>
  <?php
    $result = mysql_query("
    SELECT
        *
    FROM
        motoren
    WHERE
        merk='$merk' AND
        cat='occasions' AND
        type LIKE '$type' AND
        prijs BETWEEN '$prijs1' AND '$prijs2' AND
        km BETWEEN '$km1' AND '$km2' AND
        bouwjaar BETWEEN '$bouwjaar1' AND '$bouwjaar2'
    ORDER BY
        '$order' ASC
    ",$db);
    while ($myrow = mysql_fetch_array($result)) {
  ?>
  <tr> 
    <td bgcolor="#99ccff" width="50"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="images/<?php echo $myrow[kenteken] ?>.jpg" target=_blank>[img]"images/<?php[/img].jpg" width="50" height="50" border="0"></a></font></td>
    <td bgcolor="#99ccff" width="5%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[merk] ?>
    </font></td>
    <td bgcolor="#99ccff" width="5%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[type] ?>
    </font></td>
    <td bgcolor="#99ccff" width="9%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[bouwjaar] ?>
    </font></td>
    <td bgcolor="#99ccff" width="14%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[km] ?>
    </font></td>
    <td bgcolor="#99ccff" width="5%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[kleur] ?>
    </font></td>
    <td bgcolor="#99ccff" width="15%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[bz] ?>
    </font></td>
    <td bgcolor="#99ccff" width="4%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">&amp;euro;
    <?php echo $myrow[prijs] ?>
    </font></td>
    <td bgcolor="#99ccff" width="8%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[garantie] ?>
    </font></td>
    <td bgcolor="#99ccff" width="9%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <?php echo $myrow[kenteken] ?>
    </font></td>
    <td bgcolor="#99ccff" width="8%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="secure.php?cat=<?php echo $cat ?>&amp;action=edit&amp;edit_do=change&amp;edit_motor_id=<?php echo $myrow[motor_id] ?>&amp;order=<?php echo $order ?>">Wijzigen</a></font></td>
    <td bgcolor="#99ccff" width="13%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="secure.php?cat=<?php echo $cat ?>&amp;action=edit&amp;edit_do=delete&amp;edit_motor_id=<?php echo $myrow[motor_id] ?>&amp;order=<?php echo $order ?>">Verwijderen</a></font></td>
  </tr>
  <?php
  } //einde while
  } //einde van if $rowcount == 0
?>
</TABLE>
?>

K weet dat t veel is, maar t gaat zo wel :D

20*375Wp met Enphase IQ7+ micro's | Stiebel Eltron HGE Water/Water WP 9kW | Tesla M3, powered by SmartEVSE | Servertje @ www.coloclue.net


  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op Sunday 09 December 2001 16:41 schreef woeitje het volgende:
jah alleen heeft het hier weinig nut denk ik...
http://www.mysql.com/doc/S/t/String_comparison_functions.html
nu is ie ook case insensitive (bij mysql is dat iig zo) ;)

  • Onno
  • Registratie: Juni 1999
  • Niet online
Op Sunday 09 December 2001 17:43 schreef ACM het volgende:
nu is ie ook case insensitive (bij mysql is dat iig zo) ;)
Nope. Dat is standaard al zo.
Values in CHAR and VARCHAR columns are sorted and compared in case-insensitive fashion, unless the BINARY attribute was specified when the table was created.
bron
The only difference between BLOB and TEXT types is that sorting and comparison is performed in case-sensitive fashion for BLOB values and case-insensitive fashion for TEXT values.
bron

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op Sunday 09 December 2001 17:48 schreef Onno het volgende:
Nope. Dat is standaard al zo.
Euh, standaard voor mysql dan...

Damn, dat wist ik niet eens (ging er maar vanuit dat mysql daar wel sql compliant (?) zou zijn)
Tenminste:
code:
1
2
3
mysql> select 'a'='A';
| 'a'='A' |
|    1 |

vs (postgres)
code:
1
2
3
4
acm=# select 'a'='A';
 ?column?
----------
 f

Magoed, mijn aannames over mysql blijken keer op keer fout te zijn ;)

En dan hebben ze weer wel case sensitive tabel namen |:(

  • Onno
  • Registratie: Juni 1999
  • Niet online
Op Sunday 09 December 2001 17:54 schreef ACM het volgende:
Euh, standaard voor mysql dan...
Uiteraard. :)
En dan hebben ze weer wel case sensitive tabel namen |:(
...wat dan ook nog afhankelijk is van het OS waar je db op draait. |:(

  • Goodielover
  • Registratie: November 2001
  • Laatst online: 18-08 11:34

Goodielover

Only The Best is Good Enough.

Als $type leeg is, wordt niets geselecteerd.
type LIKE '' is false.
Verder zijn de haakjes niet nodig

btw is "type" geen reserved word en mag je dat eigenlijk helemaal niet als kolomnaam gebruiken?

Select distinct merk suggereert een niet genormaliseerd DB-ontwerp. Valt me vaak op bij web-bouwers dat DB-kennis onder niveau is

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

code:
1
order by '$blaat'

moet dat niet zijn
code:
1
order by $blaat

zonder quotes :?

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Goodielover
  • Registratie: November 2001
  • Laatst online: 18-08 11:34

Goodielover

Only The Best is Good Enough.

Volgens mij ook, ik had het nog gemist.
Zelfde geldt ook voor prijs, km en bouwjaar parameters!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

Op maandag 10 december 2001 16:44 schreef Goodielover het volgende:
Volgens mij ook, ik had het nog gemist.
Zelfde geldt ook voor prijs, km en bouwjaar parameters!
hhmmmmm, nee.

Dat zijn veldwaarden geen veldnamen

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Goodielover
  • Registratie: November 2001
  • Laatst online: 18-08 11:34

Goodielover

Only The Best is Good Enough.

Maar ik neem aan dat de parameter substitutie die wordt uitgevoerd er geen rekening mee houdt of de kolom numeriek of alfanumriek is.

dus bouwjaar BETWEEN '1990' AND '1995'
gaat waarschijnlijk wel goed(automatische conversie), maar als het numerieke velden zijn hoort er natuurlijk eigenlijk
bouwjaar BETWEEN 1990 AND 1995
te staan

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

Je kunt gewoon gequote numerieke (int,float,etc) waarden gebruiken.

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Goodielover
  • Registratie: November 2001
  • Laatst online: 18-08 11:34

Goodielover

Only The Best is Good Enough.

Als ik naar de query kijk zou ik die dynamisch opbouwen afhankelijk van de parameters waarmee de gebruiker wil zoeken.
Dus als er een prijsinterval is gespecificeerd, dan alleen prijs BETWEEN ... opnemen enz.
Ik neem aan dat kan in php.
Als ik de zoek pagina nu zie denk ik: "Je zult het bouwjaar maar niet belangrijk vinden, wat moet je dan?"
Pagina: 1