2011-08-26 32 views
2

在我的PHP腳本,我這樣做:從機器人到PHP與MySQL

$q=mysql_query($_REQUEST['query']); 

while($e=mysql_fetch_assoc($q)) 
$output[]=$e; 

print(json_encode($output)); 

mysql_close(); 

和Android的我想執行此:

nameValuePairs.add(new BasicNameValuePair("query", "SELECT name FROM RecOrg_Univ WHERE city='Rome'")); 

,我錯了嗎?

如果我把整個SELECT ....放到PHP腳本中,我只發送屬性「羅馬」它的作品,否則沒有.. :(但我需要發送一個完整的選擇...... PDO的

+0

只是一個注意:[注意SQL注入](http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain) –

+1

這是超出注入。這是完整的心臟直視手術。 – webbiedave

回答

1

實施例製備,以保護從注射

來自:[安卓]nameValuePairs.add(new BasicNameValuePair("city", "Rome"));

接收機腳本:

<?php 
$hostname = 'localhost'; 
$username = 'username'; 
$password = 'password'; 

if(isset($_REQUEST['city'])){ 
    $city=$_REQUEST['city']; 
}else{ 
    die('Missing Something...'); 
} 

$dbh = new PDO("mysql:host=$hostname;dbname=YOURDB", $username, $password); 

/*** The SQL SELECT statement ***/ 
$stmt = $dbh->prepare("SELECT name FROM RecOrg_Univ WHERE city=:city"); 
$stmt->bindParam(':city', $city); 
/**Execute it**/ 
$stmt->execute(); 

/*** fetch the results ***/ 
$result = $stmt->fetchAll(); 

/*** loop of the results and hold in an array then echo***/ 
foreach($result as $row) 
{ 
    $output[]=$row['name']; 
} 
echo json_encode($output); 

/*** close the database connection ***/ 
$dbh = null; 
?>