phppdofetchall

PHP fetchAll(PDO::FETCH_ASSOC) returns null in statement


I am trying to fetch data inserted in my database using PDO::FETCH_ASSOC.

for some reason (probably lack of understanding...) I cant echo the data from the class method.

This is the db table structure: enter image description here

I want to execute the data as an associative array of $fieldName['value'] => $fieldVal['value']

I have a class that select all data from the table by matching tz and year values. from the print _r inside the function I receive a good data. If I call the method from outside the class file I recive all values as null.

This is my class code:

function dataExist($tz, $year){
    $tz = 303748891;
    $year = 2017;
    $query = "SELECT * FROM ". $this->table_name ." WHERE tz = ? AND year = ?";

    $stmt = $this->conn->prepare($query);

    $stmt->bindParam(1, $tz);
    $stmt->bindParam(2, $year);

    // execute query
    $stmt->execute();

    //get all data
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);


    echo '<pre>';
    print_r( $result);
    echo '</pre>';

    return $result;


}

This is the good result I receive from in side the class print_r.

Array
(
    [0] => Array
        (
            [id] => 45
            [tz] => 303748891
            [fieldName] => tel
            [fieldVal] => 0547
            [year] => 2017
        )

    [1] => Array
        (
            [id] => 77
            [tz] => 303748891
            [fieldName] => fname
            [fieldVal] => Jon
            [year] => 2017
        )

    [2] => Array
        (
            [id] => 78
            [tz] => 303748891
            [fieldName] => lname
            [fieldVal] => black
            [year] => 2017
        )

    [3] => Array
        (
            [id] => 79
            [tz] => 303748891
            [fieldName] => tel
            [fieldVal] => 5555-555-55
            [year] => 2017
        )

    [4] => Array
        (
            [id] => 80
            [tz] => 303748891
            [fieldName] => mail
            [fieldVal] => aaa@aaa.aaa
            [year] => 2017
        )

    [5] => Array
        (
            [id] => 81
            [tz] => 303748891
            [fieldName] => fname
            [fieldVal] => James
            [year] => 2017
        )

    [6] => Array
        (
            [id] => 82
            [tz] => 303748891
            [fieldName] => lname
            [fieldVal] => White
            [year] => 2017
        )

    [7] => Array
        (
            [id] => 83
            [tz] => 303748891
            [fieldName] => tel
            [fieldVal] => 6669-666-666
            [year] => 2017
        )

    [8] => Array
        (
            [id] => 84
            [tz] => 303748891
            [fieldName] => mail
            [fieldVal] => bbb@bbb.bbb
            [year] => 2017
        )

)

this it the call from the other file:

$dataExist = new Form($db);
$dataExist->dataExist($tz,$year);
echo '<pre>';
print_r( $dataExist);
echo '</pre>';

I receive this:

Form Object
(
    [conn:Form:private] => PDO Object
        (
        )

    [table_name:Form:private] => submisions
    [id] => 
    [tz] => 
    [fieldName] => 
    [fieldVal] => 
    [year] => 
)

Solution

  • You aren't assigning the results of the call to your method to a variable. Change

    $dataExist->dataExist($tz,$year); 
    

    to

    $thedata = $dataExist->dataExist($tz,$year);

    then

    print_r($thedata);
    

    and you will see what you expect.