如何使用参数调用php-url,并在SQLite3上的SQL查询中使用它们(引用字符串..)


How to invoke php url with parameters and using them in SQL query on SQLite3 (quoting string ...)?

我使用的是Windows 7,在Apache 2.4上使用的是PHP版本5.6.4:我必须使用PHP在SQLite3数据库上构建查询选择。

注:我是PHP的新手。。。。。

我的代码是下面的

<?php
$comune = $_GET["comune"];
echo $comune;
echo '<br>';
echo '<br>';
$db = new SQLite3('PrezziBenzina');
if ($db) {
    $q = $db->prepare('SELECT distr.Gestore, distr.Indirizzo, distr.Bandiera, prz.descCarburante, prz.prezzo FROM anagrafica_impianti_attivi as distr join prezzo_alle_8 as prz ON (prz.idImpianto = distr.IdImpianto) WHERE distr.Comune = ?');
    $q->bindvalue(1, $comune, SQLITE3_TEXT);
    $results = $q->execute();
    while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
       print $row['Bandiera'];
       print ' -- ';
       print $row['descCarburante'];
       print ' -- ';
       print $row['prezzo'];
       print '<br>';
    }
} else {
    print "Connection to database failed!'n";
}
?>

当我使用调用程序时

http://localhost/ProvaAccessoDB-V02.php?comune=CARIGNANO

一切都很好,但当我使用调用程序时

http://localhost/ProvaAccessoDB-V02.php?comune=LA LOGGIA
http://localhost/ProvaAccessoDB-V02.php?comune=L'AQUILA
http://localhost/ProvaAccessoDB-V02.php?comune=SANT'ALBANO STURA
http://localhost/ProvaAccessoDB-V02.php?comune=AGLIE'

我的查询不起作用。

如何引用我的$comune变量来管理所有不起作用的url?

欢迎提出任何建议。提前非常感谢

Cesare

尝试。。

<?php 
//conn parameter 
$db = new PDO('sqlite:PrezziBenzina'); 
//this will set to catch error 
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 
//get url param 
$comune = $_GET['comune']; 
echo $comune; 
echo '<br>'; 
echo '<br>'; 
//set query var - OP original, nothing wrong, just testing with mine.
//$q="SELECT distr.Gestore, distr.Indirizzo, distr.Bandiera, prz.descCarburante, prz.prezzo 
//FROM anagrafica_impianti_attivi as distr 
//JOIN prezzo_alle_8 as prz ON (prz.idImpianto = distr.IdImpianto) WHERE distr.Comune = :Comune";
//set query var
$q="SELECT anagrafica_impianti_attivi.Gestore, anagrafica_impianti_attivi.Indirizzo, anagrafica_impianti_attivi.Bandiera, prezzo_alle_8.descCarburante, prezzo_alle_8.prezzo 
    FROM anagrafica_impianti_attivi
    INNER JOIN prezzo_alle_8
    ON prezzo_alle_8.idImpianto = anagrafica_impianti_attivi.IdImpianto
    WHERE anagrafica_impianti_attivi.Comune = :Comune";
//as the name suggest, it try to query and if there is error, we cath the error, these are useful during staging.   
try { 
//prepare query
$stmt = $db->prepare($q);
//bind  
$stmt->execute(array(':Comune'=>$comune, )); 
//fetch and print
while ($row = $stmt->fetch(SQLITE3_ASSOC)){
    print $row['Bandiera']; 
    print ' -- '; 
    print $row['descCarburante']; 
    print ' -- '; 
    print $row['prezzo']; 
    print '<br>'; 
    } 
}
//catch error 
catch(PDOException $e) { 
    print "Something went wrong or Connection to database failed! ".$e->getMessage();
} 
?>

玩得开心。而且,你不能通过你的url.php?comune=LA LOGGIA,在html中使用LA%LOGGIA或使用POST方法。

使用escapeString()转义字符串。

$q->bindvalue(1, $db->escapeString($comune), SQLITE3_TEXT);