I am trying to use .getJSON to post one value to a url, and have it return all corresponding values for that one value.
All values are stored in a MySQL MyISAM table.
The request is:
$.getJSON('queries.php', { key: $("#cfkey").val()},  
function(result) {
$.each(result, function(i,r) {
    console.log("user:" + r.username + "Addr:" + r.addr);
    setItem("_cfuser", r.username);
    //console.log("user:" + r.username + "saved");
});
});
cfkey, is a unique key assiged to each "user", and the idea is to get this function to return only corresponding results to that key.
PHP Code:
//error_reporting(E_ALL); ini_set("display_errors", 1);
include 'db/dbcon.php';
$input = $_GET['key'];
$input2 = $_GET['val'];
if($_GET) {
$q = "SELECT * FROM `cfaddrbook` WHERE '".$input."' = '".$input2."'";
} else {
$q = "SELECT * FROM `cfaddrbook`";
}
//Start connection with SQL
$res = $mysqli->query($q) or trigger_error($mysqli->error."[$q]");
$array = array(); // initialize
while($row = $res->fetch_array(MYSQLI_BOTH)) {
 $array[] = array(
'key' => $row[0],
'username' => $row[1],
'password' => $row[2],
'nick'     => $row[3],
'addr'     => $row[4],
'facebook' => $row[5],
'twitter'  => $row[6],
'linkedin' => $row[7],
'youtube'  => $row[8]
// ... continue like this
);
}
header('Content-Type: application/json');
echo json_encode($array);
$res->free();
$mysqli->close();
Currently, the function returns all usernames, and all addresses for one single key. I would like to send a request for a key, and get only corresponding data back, not other keys' data. I would also like to be able to input queries?key= and get back corresponding data that way as well.
EDIT: Working code:
include 'db/dbcon.php';
$thekey = $_GET['key'];
$input = $mysqli->real_escape_string($thekey);
if($_GET) {
$q = "SELECT * FROM `cfaddrbook` WHERE `key` = ".$input."";
} else {
$q = "SELECT * FROM `cfaddrbook`";
}
//Start connection with SQL
$res = $mysqli->query($q) or trigger_error($mysqli->error."[$q]");
$array = array(); // initialize
while($row = $res->fetch_array(MYSQLI_BOTH)) {
  $array[] = array(
'key' => $row[0],
'username' => $row[1],
'password' => $row[2],
'nick'     => $row[3],
'addr'     => $row[4],
'facebook' => $row[5],
'twitter'  => $row[6],
'linkedin' => $row[7],
'youtube'  => $row[8]
);
}
header('Content-Type: application/json');
echo json_encode($array);
$res->free();
$mysqli->close();
 
    