【问题标题】:from android to php to mysql从 android 到 php 到 mysql
【发布时间】:2011-08-26 22:36:56
【问题描述】:

在我的 php 脚本中我这样做:

$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 脚本中并且我只发送属性“Rome”它可以工作,否则不行.. :( 但我需要发送整个 SELECT......

【问题讨论】:

标签: php android mysql


【解决方案1】:

PDO 准备示例,以保护您免受注射。

发件人:[andriod] 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;
?>

【讨论】:

    猜你喜欢
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2012-01-22
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多