我在Windows 7上,在Apache 2.4上使用PHP 5.6.14版:我必须使用PHP在SQLite3数据库上构建查询选择。
NOTA:我是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?
任何建议表示赞赏。提前非常感谢你
切萨雷
最佳答案
尝试..
<?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方法。
关于php - 如何调用带参数的php url并在SQLite3上的SQL查询中使用它们(引号…)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959570/