Tenho um aplicativo MariaDb/PHP usado para pesquisar nomes biológicos (latino/sueco) de um grande banco de dados científico. A única entrada textual do usuário é fornecer um nome (parcial) em um textfield
, lido pelo PHP e inserido em uma consulta SQL (abreviada):
Select ... from Database.table x WHERE Condition AND MATCH
(x.scientific_name) AGAINST ('"full name"' IN BOOLEAN
MODE) ;
ou
Select ... from Database.table x WHERE Condition
AND x.scientific_name LIKE '%partial_name%' AND
!isnull(x.scientific_name ;
Essas consultas retornam 0 ou mais resultados do database.table, definidos por:
scientific_name
varchar(255)
Atualmente não estou usando consultas parametrizadas (preciso fazer isso?)
(MariaDb ver. 11.6.2, PHP ver. 8.2, servidor Debian 12 com Apache2)
EDIT: Código PHP:
//User input:
<input type='search' name='srcfield' id='srcfield' value='' size='45' maxlength='55' autofocus placeholder="Sök efter organism/taxon... (fågelnamn)" style='font-size:14px;background-color: #DCF7E7;'
oninput='fonblur1(srcfield)'>
//returns srcfield to a PHP var => $safein1 via js:
function fonblur1() {
var x = document.getElementById("srcfield");
...
}
//A large amount of various non-text input is used to build the dynamic SQL, e.g:
<input type="checkbox" id="cb02" name ="exakt2" onclick="" >
<label for="cb02">search exact/whole name</label></span>
//Dynamic SQL (untranslated code):
$sqlsrc = "SELECT f.`TaxonId`,f.`Taxonkategori`,f.`Vetenskapligt_namn0`,
f.`URL_till_taxoninformation` AS `URL_till_taxoninformation`,
d.Familj AS Familj, d.Slakte AS Slakte,
d.TaxonId AS TaxonId2, t.taxid, 1 AS accept , 'nosyn' AS syn
concat(d.Rike,' -› ', d.Fylum,' -› ',IFNULL(d.Klass, '~')) AS grupp,
FROM namndata.ftextsrc f
JOIN namndata.taxonid0 t using (Taxonkategori)
JOIN namndata.taxon0_T d USING (TaxonId)
WHERE f.`Vetenskapligt_namn0` LIKE '%" . $safein1 . "%' " .
" AND d.TaxonStatus='Accepterat' AND d.".$roww2['Taxonkategori'] ." = '".$roww2['Vetenskapligt_namn']."'
" . $TxIdf . "
order by grupp, d.radnr, f.`Vetenskapligt_namn0` LIMIT 0,2500;";
// Actual code, sent to MariaDb:
SELECT f.`TaxonId`,f.`Taxonkategori`,f.`Vetenskapligt_namn0`,
f.`URL_till_taxoninformation` AS `URL_till_taxoninformation`,
d.Familj AS Familj, d.Slakte AS Slakte, 'nosyn' AS syn,
d.TaxonId AS TaxonId2, t.taxid, 1 AS accept, concat(d.Rike,' -› ', d.Fylum,' -› ',IFNULL(d.Klass, '~')) AS grupp
FROM namndata.ftextsrc f JOIN namndata.taxonid0 t using (Taxonkategori)
JOIN namndata.taxon0_T d USING (TaxonId)
WHERE MATCH (f.`Vetenskapligt_namn0`) AGAINST ('"carnivora"' IN BOOLEAN MODE)
AND d.TaxonStatus='Accepterat' AND d.Fylum = 'Chordata' order by grupp, d.radnr, f.`Vetenskapligt_namn0` LIMIT 0,2500;