mysqli中的UNION准备声明不返回只有一个值而不是两个值

时间:2014-04-05 16:42:46

标签: php mysqli

PHP

$wall_id = 5;

    $pull_adj_id = $connectDB->prepare("(SELECT walls.wall_id FROM walls WHERE 
                       walls.wall_id > ? ORDER BY walls.wall_id ASC LIMIT 1)
                       UNION
                       (SELECT walls.wall_id FROM walls WHERE 
                       walls.wall_id < ? ORDER BY walls.wall_id DESC LIMIT 1);");
    $pull_adj_ids->bind_param('ii', $wall_id, $wall_id);
    $pull_adj_ids->execute();
    $adj_result     = $pull_adj_ids->get_result();
    $adj_data       = $adj_result->fetch_array();
    print_r($adj_data);

输出:Array ( [0] => 5 [wall_id] => 5 )

关于我之前的问题,我尝试构建我的查询以从表中提取大于和小于id,但查询只返回一个值。但是当我尝试在phpMyAdmin中运行相同的查询时,它会提供64

2 个答案:

答案 0 :(得分:1)

当您只提取一个

时,您的查询会返回2行

答案 1 :(得分:0)

你必须循环你的结果。

试试这个

$pull_adj_ids->execute();
$pull_adj_ids->store_result();
$adj_result     = $pull_adj_ids->bind_result($wall_id); 
while ($adj_data = $adj_result->fetch()){
     echo $wall_id.'<br />';

  }