0

I want to convert severals rows after PDO request but I can't display result on my page.

This is for use with a rubimotion app

there is my php code:

<?php
    //to see what return request in my php page
    header('Content-type: text/html; charset=utf-8');

    try {
        $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
        $bdd = new PDO('mysql:host=IP;dbname=NAME','USER','PASS');

        $reponse = $bdd->prepare("SELECT * FROM table WHERE id = 1");
        $reponse->execute(); #this request return severals rows

        $nb = $reponse->rowCount();
        if($nb > 0){
            $json = json_encode( $reponse->fetchAll( PDO::FETCH_ASSOC ) );
            echo $json;
        }
    }
    catch (Exception $e) {
        echo "Connexion échouée : " . $e->getMessage();
    }

?>

But I have a white page and in the console the response is empty.

Whats wrong ?

I tryed many option however I have many errors like

  • array to string convertion
  • SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

thanks ! And sorry for the mistakes I am french

11
  • Check if you have something like single quote (') or any other character in your dataset ... Commented Aug 22, 2017 at 13:20
  • check row count by echo $nb; first for result data is available or not.Or add else part of if($nb > 0) condition Commented Aug 22, 2017 at 13:20
  • I don't have single quote juste many fields string @BharatDangar : echo $nb -> 218 rows ! Commented Aug 22, 2017 at 13:24
  • Have you enabled error reporting in PHP? Do you see any errors in your web server error log? Commented Aug 22, 2017 at 13:24
  • Did you realize you have defined $pdo_options, but it isn't used in your connection? Commented Aug 22, 2017 at 13:26

1 Answer 1

0

You must removed header function & use below line. You should add below line above the json_encode function.

header("content-type:application/json");
Sign up to request clarification or add additional context in comments.

3 Comments

now I have $json = json_encode( $reponse->fetchAll( PDO::FETCH_ASSOC ) ); var_dump($json); in my page there is bool(false)
header("content-type:application/json"); $reponse = $bdd->prepare("SELECT * FROM manager_vtours WHERE user_id = 1"); echo json_encode($reponse->fetchAll(PDO::FETCH_ASSOC)); result = []
I don't have error... juste an empty response in my console and a white page

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.