localhero Posted April 11, 2008 Report Share Posted April 11, 2008 Ir forma ar vairākiem laukiem teiksim: id, vards, amats. Ir pieprasijums SELECT FROM pasutijumi WHERE id LIKE '%$id%' AND vards LIKE '%$vards%' AND amats LIKE '%$amats%'. Problēma tāda ka id lauka ierakstot 4 atgriezh gan 4 gan 44, gan 14 gan 40 utt, tas pats ar vards vai amats. LIKE vieta likt "=", bet lieta tada ka ne vienmer visi lauki ir aizpilditi meklesanas forma. Varetu konstruet katram gadijumam savu vaicajumu bet iespejams ar laiku bus kadi 8 vai 9 parametri. Link to comment Share on other sites More sharing options...
Vebers Posted April 11, 2008 Report Share Posted April 11, 2008 Konstruē tikai where daļu... Link to comment Share on other sites More sharing options...
black Posted April 11, 2008 Report Share Posted April 11, 2008 (edited) Laikam jau nesaprotu, kur ir problēma. P.S. Jauka profila bilde (smuki zobi!) Edited April 11, 2008 by black Link to comment Share on other sites More sharing options...
Gints Plivna Posted April 11, 2008 Report Share Posted April 11, 2008 Parametriem likt %parametrs% vispār normāli ir nepieciešams tikai krustvārdu mīklām, kur ir zināms, ka 3 burts ir a un piektais b, visur citur tā 99.99% gadījumu ir lieka greznība, kas maksā ļoti dārgi. tātad īsumā: 1) vajag katru parametru apskatīt atsevišķi un katrā ziņā pēc iespējas izvairīties no %bla%. Jo sevišķi tas nav vajadzīgs kaut kādiem idu laukiem un klasifikatoru laukiem. 2) ka jau teikts konstruēt where klauzu atsevišķi un likt tikai tos nosacījumus, kas patiešām ir aizpildīti. Un maksimums censties likt vai nu vienādību vai like bla% (parametra iesākums ir definēts). Gints Plivna http://datubazes.wordpress.com Link to comment Share on other sites More sharing options...
localhero Posted April 11, 2008 Author Report Share Posted April 11, 2008 (edited) Konstruē tikai where daļu... Ka to izdarit? Tiri sintaktiski. Edited April 11, 2008 by localhero Link to comment Share on other sites More sharing options...
Vebers Posted April 11, 2008 Report Share Posted April 11, 2008 (edited) $where = array(); if(!empty($_POST['id'])) $where[] = "id = ".mysql_escape_string($_POST['id']); if(!empty($_POST['name'])) $where[] = "name = '".mysql_escape_string($_POST['name'])."'"; $query = (!empty($where)) ? "select bla, bla2 from blabla where ".implode(', ', $where) : "select bla, bla2 from bla"; Edited April 11, 2008 by Vebers Link to comment Share on other sites More sharing options...
Gints Plivna Posted April 11, 2008 Report Share Posted April 11, 2008 a kas notiks, ja kritēriju nebūs vispār? ;) Link to comment Share on other sites More sharing options...
Vebers Posted April 11, 2008 Report Share Posted April 11, 2008 Gints Plivna, pielaboju ;) Bet nu man prasīja tīri sintaksisti / idejiski - domāju, ka no tā piemēra ideja skaidra :) Link to comment Share on other sites More sharing options...
localhero Posted April 11, 2008 Author Report Share Posted April 11, 2008 Paldies visiem jaukajiem cilvecinjiem kas palidzeja :) Link to comment Share on other sites More sharing options...
localhero Posted April 11, 2008 Author Report Share Posted April 11, 2008 Neliels labojums , vieta ieliekam AND: $where = array(); if(!empty($_POST['id'])) $where[] = "id = ".mysql_escape_string($_POST['id']); if(!empty($_POST['name'])) $where[] = "name = '".mysql_escape_string($_POST['name'])."'"; $query = (!empty($where)) ? "select bla, bla2 from blabla where ".implode(' [b]AND[/b] ', $where) : "select bla, bla2 from bla"; Link to comment Share on other sites More sharing options...
Recommended Posts