2
我希望我的下面的PDO選擇與底部的兩個IF語句一起工作?
第一次如果我只是想確保沒有錯誤。
第二個IF我想檢查它返回的行數。我知道這number of rows == 0
將無法正常工作。
有沒有辦法做到這一點?PHP:如何讓我的IF語句與PDO select一起使用?
try {
$conn = new PDO('mysql:host=localhost;dbname=zs', 'zs', 'rlkj08sfSsdf');
$conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
$stmt = $conn->prepare("SELECT * FROM zip WHERE zip_code =:zip1");
$stmt->bindValue(':zip1', $_POST[zipcode], PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($rows = "") {
echo "<p><strong>There was a database error attempting to retrieve your ZIP Code.</strong></p>\n";
}
if(number of rows == 0) {
echo "<p><strong>No database match for provided ZIP Code.</strong> Please enter a new ZIP Code.</p>\n";
}
'if($ rows =「」){'這是一項任務,而不是比較。 –
'fetchAll()'返回一個數組。你可以使用'empty()'或'count()'來測試它是否有任何東西。 –
不要考慮郵編'int's,你會遇到前導零的問題。 https://stackoverflow.com/questions/893454/is-it-a-good-idea-to-use-an-integer-column-for-storing-us-zip-codes-in-a-databas – chris85