2016-12-16 71 views
1

如何使用具有多個條件的PDO語句填充我的數組?PHP PDO多重選擇,多重條件

$stmt = $conn->prepare("SELECT * 
         FROM the_table 
         WHERE stmt1 = '$value1' 
          AND stmt2 = '$value2' 
         ORDER BY sent_date"); 
$stmt->execute(); 
$row = $stmt->fetchAll(); 

所以,這個工作,但我需要我的表中的表數據,因爲我只會得到第一個語句值。我的意思是?如果我的第一個值(如$ value1)超出了對第二個語句值的選擇更改(如$ value2)。我會得到我所有的表格數據,其中存在$ value1和$ value2 我需要像$ row這樣的數組值數值

對不起,對於這個組合:D。 在此先感謝。

+0

這不是你如何使用預處理語句..我也不知道你的問題是什麼。 – chris85

+0

歡迎來到SO。 請閱讀[我可以問哪些主題](http://stackoverflow.com/help/on-topic) 和[如何提出一個好問題](http://stackoverflow.com/help/how-to - 問) 和[完美的問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何創建[最小,完整和可驗證的例子] (http://stackoverflow.com/help/mcve) – RiggsFolly

+0

你不明白什麼? – Chraze

回答

0

基本上,你會準備好您的查詢像

<?php 

/* insert code for database connexion here */ 

$value1 = 'something'; 
$value2 = 'whatever'; 

/* The query itself */ 
$query = $connexion->prepare('SELECT * FROM the_table WHERE stmt1 = :first AND stmt2 = :second ORDER BY sent_date'); 

/* Now we add the wanted values to our query */ 
$query->execute([ 
    'first' => $value1, 
    'second' => $value2 
]); 

$result = $query->fetchAll(); 

不要忘記檢查,如果結果是你所期望的。