[英]PHP fetchAll(PDO::FETCH_ASSOC) returns null in statement
I am trying to fetch data inserted in my database using PDO::FETCH_ASSOC
. 我正在尝试使用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: 这是db表结构:
I want to execute the data as an associative array of $fieldName['value'] => $fieldVal['value']
我想将数据作为$fieldName['value'] => $fieldVal['value']
的关联数组执行
I have a class that select all data from the table by matching tz
and year
values. 我有一个通过匹配tz
和year
值从表中选择所有数据的类。 from the print _r inside the function I receive a good data. 从函数中的print _r我收到了很好的数据。 If I call the method from outside the class file I recive all values as null. 如果我从类文件外部调用该方法,则我会将所有值都检索为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
. 这是我从类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] =>
)
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. 您将看到您的期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.