php - 如何调用带参数的php url并在SQLite3上的SQL查询中使用它们(引号…)?

标签 php sqlite apache2.4 php-5.6

我在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/

相关文章:

c - Visual Studio 未解析的符号 _InterlockedCompareExchange

php - Wordpress 环境和极高的使用率 (PHP-FPM)

webserver - 如何使用自定义文档根在 Apache 2.4 中显示目录索引

python - 允许/阻止访问特定路径,apache 2.4 失败

php - 刷新注册页面时,为什么在表中添加空行。甚至没有填写表格

php - 使用 PHP 操作 MySQL 数据库

PHP - 按键长度对哈希数组进行排序

python - 如何在 python 中创建一个包含 1 行的 sqlite3 数据库

php - 在 PHP 中将 Favicon 转为 PNG

android - 在 Android 上,主键的 getColumnIndex 在 sqlite 上返回值 -1