1

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;
    }
8
  • Is there a particular error that it gives as per mysqli_connect_error()? Commented May 13, 2013 at 16:29
  • Could you, please, do a echo $query; and show us the value printed? Commented May 13, 2013 at 16:31
  • Instead of var_dump(names), put an echo $names, just to see if it is well constructed Commented May 13, 2013 at 16:31
  • Are you getting that error when you load the page in the browser? Check that the output is a well-formed json (that var_dump, for instance, would break it), because the device might not be able to parse it and might be returning null Commented May 13, 2013 at 16:35
  • @Alex yes commenting out $vardumbs worked for the null value part now I can get $decoded result in device, but still query fails. Commented May 13, 2013 at 16:55

1 Answer 1

1

I see you are mixing the syntax of mysqli.

$sql = new mysqli($config['localhost'], $config['xxx'], $config['xxxx'],   $config['xxxxx']);
if ($mysqli->connect_error) {

You should use the object oriented notation to correspond to using new. Also, how you are writing the config seems kind of strange, are you sure the contents of the config are being obtained correctly?

Sign up to request clarification or add additional context in comments.

1 Comment

credentials are correct but change the method to $sql = new mysqli('localhost', 'xxxx', 'xxxxx', 'xxxxx'); if (mysqli_connect_errno()) solved the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.