I have been trying to use JSON in my iPhone app to retrieve information from my SQL database with the use of PHP. I can successfully send a JSON and return it to the iPhone app. My problem occurs when I try to take the parameters in from the JSON array that was sent. It fails and I am not sure what is wrong with my statements. Any suggestions would be great.
function connect($config) {
try {
$conn = new \PDO('mysql:host=localhost;dbname=authorization',
$config['username'],
$config['password']);
$conn->setAttribute(\PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}catch(Exception $e){
return false;
}
}
function query($conn, $body) {
if (isset($body['authorization'])) {
// Put parameters into local variables
$authorization_code = $body['authorize'];
$device_id = $body['device'];
}
$stmt = $conn->prepare("SELECT uses_remaining FROM phone_authorization WHERE authorization_code = $authorization_code");
$stmt->execute();
$results = $stmt->fetchAll();
return $results ? $results : false;
}
if ($_SERVER['HTTP_METHOD'] === 'postValues'){
$body;
if($_POST == null){
$handle = fopen('php://input', 'r');
$rawData = fgets($handle);
$body = json_decode($rawData);
}
else{
$body == $_POST;
}
$conn = connect($config);
$row = query($conn, $body);
if ($row) {
echo json_encode($row);
}
else
{
$data['value1'] = $row;
$data['value2'] = "test";
echo json_encode($data);
}
}
else {
$data['error'] = 'The Service you asked for was not recognized';
echo json_encode($data);
}
Thanks