Trying to send few file names to server via php script from ios device using json.
I can send file names and receive them back if I use:
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
sendResponse(200, json_encode($decoded));
I sent a json string from ios like= ["sample.pdf","sample-1.pdf"]
sendResponse 200 is in ios device which proves my send and receive methods working=
(
"sample.pdf",
"sample-1.pdf"
)
However If I change the code to below and try to run a query I always get Query failed
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
var_dump($decoded);
$names = join("', '",$decoded);
var_dump($names);
$sql = new mysqli($config['localhost'], $config['xxx'], $config['xxxx'], $config['xxxxx']);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
$query = "SELECT * FROM FILES WHERE name IN ('$names')";
$result = $sql->query($query);
if (!$result) {
var_dump($result);
//printf("Query failed: %s\n", $mysqli->error);
//sendResponse(417, json_encode("Query failed:"));
sendResponse(417, json_encode($decoded));
exit;
}
$rows = array();
while($row = $result->fetch_row()) {
$rows[]=$row;
}
sendResponse(200, json_encode($decoded));
$result->close();
$sql->close();
From the above code I always get Query failed response, but real strange thing is that I also get NULL response from sendResponse(417, json_encode($decoded)) or sendResponse(200, json_encode($decoded));
Why do I get null after query, after all I did not change $decode variable?
Why does the query fails?
EDIT:::::
removing removing vardumbs for correct json result and changing connection code to this solved the problem know I can get correct query result.
$sql = new mysqli('localhost', 'user', 'password', 'database');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
echo $query;and show us the value printed?$vardumbsworked for thenullvalue part now I can get$decodedresult in device, but still query fails.