我正在尝试将下表中的值分配给2个单独的变量:
$ a_intro =来自d的值,如果c为空& $ a_features =来自d的值,如果c不为空
但下面的代码给了我 - UNDEFINED VARIABLE $ a_features。
TABLE STRUCTURE IS :
id1 | id2 | a | b | c | d
------------------------------------------------------------------------
11 22 1 2 text1
11 22 1 2 3 text2
$query3 = "SELECT * FROM table WHERE (id1=11 AND id2=22)";
if($result3 = $dbo->query($query3)){
//echo $result3->fetchColumn()."<br/>";
$data3 = $result3->fetch(PDO::FETCH_ASSOC);
if(empty($data3['c'])){
$a_intro = $data3['d'];
//} else if(!empty($data3['c'])){
} else {
$a_features = $data3['d'];
//echo "Its not EMPTY";
}
} else {
echo "ERROR in SQL Query for fetching Values<br/>";
echo mysqli_error($dbo);
}
任何猜测,什么错?在此先感谢..
答案 0 :(得分:0)
由于您希望在结果集中获得多行,因此需要在循环中处理结果
您似乎也在混合PDO和MYSQLI API,如果您在连接数据库时使用的话,那么它将无法使用PDO
此外,您的IF不会涵盖所有可能性,并且会导致变量在某些情况下未定义和未设置。
$query3 = "SELECT * FROM table WHERE (id1=11 AND id2=22)";
$result3 = $dbo->query($query3);
if($result3 === FALSE) {
echo "ERROR in SQL Query for fetching Values<br/>";
print_r($dbo->errorInfo();
exit;
}
// pre define and initialize the variables
$a_intro = [];
$a_features = [];
// this loop is implied by your query to get 2 or more results
// However it is nonsence as you are processing 2 rows into
// scalar variables $a_intro and $a_features
// maybe they shoud be arrays
while ($data3 = $result3->fetch(PDO::FETCH_ASSOC)) {
if(empty($data3['c'])){
$a_intro[] = $data3['d'];
} else {
$a_features[] = $data3['d'];
}
}
由于我不知道你用$ a_intro和$ a_features做了什么,在这方面很难提供更多帮助