在本教程中,您將學(xué)習(xí)如何使用PHP根據(jù)特定條件從MySQL數(shù)據(jù)庫表中讀取記錄。
WHERE子句僅用于提取滿足指定條件的那些記錄。
WHERE子句的基本語法可以通過以下方式給出:
SELECT column_name(s) FROM table_name WHERE column_name operator value
讓我們使用SELECT語句中的WHERE子句進行一個SQL查詢,然后通過將其傳遞給PHP mysqli_query()函數(shù)來執(zhí)行該查詢,以獲取過濾后的數(shù)據(jù)。
假設(shè)demo數(shù)據(jù)庫中有一個persons表,它有以下記錄:
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ | 1 | Peter | Parker | peterparker@mail.com | | 2 | John | Rambo | johnrambo@mail.com | | 3 | Clark | Kent | clarkkent@mail.com | | 4 | John | Carter | johncarter@mail.com | | 5 | Harry | Potter | harrypotter@mail.com | +----+------------+-----------+----------------------+
以下PHP代碼從persons表中讀取 first_name ='john' 的所有行:
<?php /* 嘗試MySQL服務(wù)器連接。 假設(shè)您正在運行MySQL。 具有默認設(shè)置的服務(wù)器(沒有密碼的用戶“root”) */ $link = mysqli_connect("localhost", "root", "", "demo"); //檢查連接 if($link === false){ die("錯誤:無法連接。 " . mysqli_connect_error()); } //嘗試選擇查詢執(zhí)行 $sql = "SELECT * FROM persons WHERE first_name='john'"; if($result = mysqli_query($link, $sql)){ if(mysqli_num_rows($result) > 0){ echo "<table>"; echo "<tr>"; echo "<th>id</th>"; echo "<th>first_name</th>"; echo "<th>last_name</th>"; echo "<th>email</th>"; echo "</tr>"; while($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['first_name'] . "</td>"; echo "<td>" . $row['last_name'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } echo "</table>"; // 關(guān)閉結(jié)果集 mysqli_free_result($result); } else{ echo "找不到與您的查詢匹配的記錄。"; } } else{ echo "錯誤:無法執(zhí)行 $sql. " . mysqli_error($link); } //關(guān)閉連接 mysqli_close($link); ?>
<?php /* 嘗試MySQL服務(wù)器連接。 假設(shè)您正在運行MySQL。 具有默認設(shè)置的服務(wù)器(沒有密碼的用戶“root”) */ $mysqli = new mysqli("localhost", "root", "", "demo"); //檢查連接 if($mysqli === false){ die("錯誤:無法連接。 " . $mysqli->connect_error); } //嘗試選擇查詢執(zhí)行 $sql = "SELECT * FROM persons WHERE first_name='john'"; if($result = $mysqli->query($sql)){ if($result->num_rows > 0){ echo "<table>"; echo "<tr>"; echo "<th>id</th>"; echo "<th>first_name</th>"; echo "<th>last_name</th>"; echo "<th>email</th>"; echo "</tr>"; while($row = $result->fetch_array()){ echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['first_name'] . "</td>"; echo "<td>" . $row['last_name'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } echo "</table>"; //釋放結(jié)果集 $result->free(); } else{ echo "找不到與您的查詢匹配的記錄。"; } } else{ echo "錯誤:無法執(zhí)行 $sql. " . $mysqli->error; } //關(guān)閉連接 $mysqli->close(); ?>
<?php /* 嘗試MySQL服務(wù)器連接。 假設(shè)您正在運行MySQL。 具有默認設(shè)置的服務(wù)器(沒有密碼的用戶“root”) */ try{ $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", ""); //將PDO錯誤模式設(shè)置為異常 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ die("錯誤:無法連接。 " . $e->getMessage()); } //嘗試選擇查詢執(zhí)行 try{ $sql = "SELECT * FROM persons WHERE first_name='john'"; $result = $pdo->query($sql); if($result->rowCount() > 0){ echo "<table>"; echo "<tr>"; echo "<th>id</th>"; echo "<th>first_name</th>"; echo "<th>last_name</th>"; echo "<th>email</th>"; echo "</tr>"; while($row = $result->fetch()){ echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['first_name'] . "</td>"; echo "<td>" . $row['last_name'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } echo "</table>"; //釋放結(jié)果集 unset($result); } else{ echo "找不到與您的查詢匹配的記錄。"; } } catch(PDOException $e){ die("錯誤:無法執(zhí)行 $sql. " . $e->getMessage()); } //關(guān)閉連接 unset($pdo); ?>
過濾后,結(jié)果集將如下所示:
+----+------------+-----------+---------------------+ | id | first_name | last_name | email | +----+------------+-----------+---------------------+ | 2 | John | Rambo | johnrambo@mail.com | | 4 | John | Carter | johncarter@mail.com | +----+------------+-----------+---------------------+