2015-11-27 1 views
1

Je suis sur Windows 7, en utilisant PHP Version 5.6.14 sur Apache 2.4: Je dois construire une requête select sur une base de données SQLite3 en utilisant un PHP.Comment appeler l'URL php avec des paramètres et les utiliser dans une requête SQL sur SQLite3 (chaîne de caractères ...)?

NOTE: Je suis un newbye sur PHP .....

Mon code est le suivi

<?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"; 
} 
?> 

Quand j'appelle ma procédure en utilisant

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

tout fonctionne très bien , mais lorsque j'appelle ma procédure en utilisant

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' 

ma requête ne fonctionne pas.

Comment puis-je citer/dissocier ma variable $ comune pour gérer toute l'URL qui ne fonctionne pas?

Toutes les suggestions sont appréciées. Merci beaucoup à l'avance

Cesare

+0

puisque vous utilisez sqlite, essayez d'échapper votre apostrophe 'avec apostrophe. ce qui signifie fondamentalement '', la variable en cours de définition doit être stockée avec L'AQUILA, essayez $ comune = str_replace ("'", "' '", $ _ GET [' comune ']); et voir comment –

+0

Juste essayé, mais ne fonctionne toujours pas ... :-(Merci en tout cas! – Cesare

+0

faire un appel d'url directement par http: //localhost/ProvaAccessoDB-V02.php? comune = L'AQUILA et voir si cela fonctionne –

Répondre

1

Essayez ..

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

Amusez-vous. aussi, vous ne pouvez pas passer yoururl.php? Comune = LA LOGGIA, utilisez LA% LOGGIA en html ou utilisez la méthode POST à ​​la place.

+0

Ça marche !! Excellente solution! – Cesare

0

échapper à la chaîne à l'aide escapeString().

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

Uhmmm .. J'ai essayé mais pas de bons résultats. Y at-il un moyen d'imprimer/montrer la requête exécuté? $ q dans mon code ce n'est pas une chaîne, donc je ne peux pas voir la requête .... Merci à n'importe quel cas! – Cesare