2015-10-12 28 views
0
$prepare=$text->prepare(" 
    select * 
    from (
    select text1,text2 
    from test1 
    union 
    select text1,text2 
    from test2 
) as u 
    where u.text1 in (?,?)"); 
$prepare->execute($array); 
$fetch=$prepare->fetch(PDO::FETCH_ASSOC); 
print_r($fetch); 

我有一個在所有使用UNION,WHERE和IN的PDO語句。這應該返回一些結果。該數組包含所有數據點。聲明中可能存在什麼問題?PDO聯合在哪裏和在

回答

0

在準備好的語句中,在IN子句中,您需要自己構造查詢。有關更多信息,請參閱此post。 如果您遇到錯誤,請修改您的帖子並添加錯誤消息。

編輯:

//Correct use of fetch statement: 
$prepare->execute($array); 
$results = array(); 
while($fetch=$prepare->fetch(PDO::FETCH_ASSOC)){ 
    $results[] = $fetch; 
} 
print_r($results); 
+0

確定。但我嘗試了沒有IN的測試。只要做「select * from(select text1,text2 from test1)as u」,當數據庫中有8個以上時,我只得到1個結果。 –

+0

只有「select text1,test1 from test1」才能得到多少行? –

+0

你只有一行嘗試從PHP或從MySQL控制檯?您錯誤地使用了獲取語句 –