简体   繁体   中英

Get JSON data from php with $_GET request in android

Here is my PHP code:

  $query = $sdb->prepare("SELECT * FROM `t_comments` WHERE `a_link` = ?");
    $query->bindValue(1, $_GET["link"]);
    $query->execute();

and here is my JSON request in Android:

JSONObject json = jParser.makeHttpRequest("http://serverip/json/getcomments.php?link=" + index, "GET", params);

Actually, this code works if I remove link request in Activity. Also, if I replace $_GET with ID in PHP, but index will change on each request so.

SOLVED: i needed to use Params list to add GET parameter inside simply with params.add(new BasicNameValuePair("STRING NAME", SOMETHING TO STORE));

$query = $sdb->prepare("SELECT * FROM `t_comments` WHERE `a_link` = :link");
$query->bindValue(':link', $_GET["link"]);
$query->execute();

From the documentation:

<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->bindValue(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', $colour, PDO::PARAM_STR);
$sth->execute();
?>

In your code:

if(isset($_GET["link"])){
    $aVarName = $_GET["link"];
    $query->bindValue(1, $aVarName, PDO::PARAM_INT);
} else {
    // Some error or default value.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM