0 レビュー
2 回答
sqlite-パラメータを使用してphpurlを呼び出し、SQLite3のSQLクエリでそれらを使用する方法(文字列を引用...)?
私はWindows7を使用しており、Apache 2.4でPHPバージョン5.6.14を使用しています。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を管理するにはどうすればよいですか?
どんな提案でも大歓迎です。よろしくお願いします
チェザーレ
わからない
0
レビュー
答え :
解決策:
試してみてください。
<?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メソッドを使用することはできません。
わからない
0
レビュー
答え :
解決策:
escapeString()
を使用して文字列をエスケープします。
$q->bindvalue(1, $db->escapeString($comune), SQLITE3_TEXT);
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。