從PDO返回值我正在從基於mySQL的php遷移到PDO。我在做一個if語句。基本上,我想查詢數據庫中的值,並檢查是否有任何東西被返回。PHP:根據語句
與MySQL我會做這樣的:
//Query the entered data to see if it matches
$myQuery = "SELECT * FROM $tbl_name WHERE myData ='$MyData'";
$myResult = mysql_query($myQuery);
// If statement
if(mysql_num_rows($myResult) >=1) {
$result .= "The data matched and SQL query was successful";
}
隨着PDO我有這個迄今爲止,雖然它不工作尚未:
//Query the entered data to see if it matches
$myQuery = "SELECT myData FROM table_name WHERE myData = '$myData'";
$myResult = $db->single($myQuery);
// If statement
if($myResult == 1) {
$result .= "The data matched and PDO query was successful";
}
雖然,從我看到你不不需要將查詢和結果與PDO分開,並且可以將查詢和結果組合到一個語句中。
並非如此。這是我用於在使用PDO和MySQL時返回行計數的事實上的方法,它可以在100%的時間內運行。 – silkfire
你正在與手冊爭論? Mkay,我只是把我的5美分放在那裏......而且顯然你甚至沒有閱讀完整的答案......或者去看手冊以查看那裏的相同文本......它爲你工作的事實並不意味着它適用於所有人。我已經在很多問題中看到過這個問題。 –
它只需要一點谷歌搜索 - 像這樣一個:https://stackoverflow.com/questions/883365/row-count-with-pdo和這一個https://stackoverflow.com/questions/6041886/count-數 - 的 - 行 - 中 - 選擇 - 查詢與-PDO –