如何使用参数调用 php url 并在 SQL ite3 的 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.14:我必须使用 [= 在 SQLite3 数据库上构建查询 select 34=].

注意:我是 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?

如有任何建议,我们将不胜感激。非常感谢您

切萨雷

使用 escapeString().

转义字符串
$q->bindvalue(1, $db->escapeString($comune), SQLITE3_TEXT);

试试..

<?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();
} 
?>

玩得开心。 此外,您不能通过 yoururl.php?comune=LA LOGGIA,在 html 中使用 LA%LOGGIA 或使用 POST 方法。