English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to read records from a MySQL database table using PHP based on specific conditions.
WHEREThe clause is used only to extract those records that meet the specified conditions.
The basic syntax of the WHERE clause can be given as follows:
SELECT column_name(s) FROM table_name WHERE column_name operator value
Let's use the WHERE clause in the SELECT statement to perform an SQL query, and then execute the query by passing it to the PHP mysqli_query() function to obtain the filtered data.
Assuming there is a persons table in the demo database, it has the following records:
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ | 1 | Peter | Parker | [email protected] | | 2 | John | Rambo | [email protected] | | 3 | Clark | Kent | [email protected] | | 4 | John | Carter | [email protected] | | 5 | Harry | Potter | [email protected] | +----+------------+-----------+----------------------+
The following PHP code frompersonsRead all rows from the table where first_name = 'john':
<?php /* Attempt to connect to MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without a password) */ $link = mysqli_connect("localhost", "root", "", "demo"); //Check connection if($link === false){ die('Error: Unable to connect.' . mysqli_connect_error()); } // $sql = \ * FROM persons WHERE first_name='john"; if($result = mysqli_query($link, $sql)){ if(mysqli_num_rows($result) > 0){ echo \ echo \/ / echo \/ / echo \/tr>"; while($row = mysqli_fetch_array($result)){ echo \ echo \/td>"; echo \/td>"; echo \/td>"; echo \/td>"; echo \/tr>"; } echo \/table>"; // Close result set mysqli_free_result($result); } else{ echo \ } } else{ echo 'Error: Unable to execute $sql.' . mysqli_error($link); } //Close connection mysqli_close($link); ?>
<?php /* Attempt to connect to MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without a password) */ $mysqli = new mysqli("localhost", "root", "", "demo"); //Check connection if($mysqli === false){ die('Error: Unable to connect.' . $mysqli->connect_error); } // $sql = \ * FROM persons WHERE first_name='john"; if($result = $mysqli->query($sql)){ if($result->num_rows > 0){ echo \ echo \/ / echo \/ / echo \/tr>"; ->fetch_array()){ echo \ echo \/td>"; echo \/td>"; echo \/td>"; echo \/td>"; echo \/tr>"; } echo \/table>"; //Release result set $result->free(); } else{ echo \ } } else{ echo 'Error: Unable to execute $sql.' . $mysqli->error; } //Close connection $mysqli->close(); ?>
<?php /* Attempt to connect to MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without a password) */ $pdo = new PDO(\ //Set PDO error mode to exception $pdo- } catch(PDOException $e){ ->getMessage()); } // $sql = \ * FROM persons WHERE first_name='john"; $result = $pdo->query($sql); if($result->rowCount() > 0){ echo \ echo \/ / echo \/ / echo \/tr>"; - echo \ echo \/td>"; echo \/td>"; echo \/td>"; echo \/td>"; echo \/tr>"; } echo \/table>"; //Release result set unset($result); } else{ echo \ } } catch(PDOException $e){ die("Error: Unable to execute $sql. ". $e->getMessage()); } //Close connection unset($pdo); ?>
After filtering, the result set will be as follows:
+----+------------+-----------+---------------------+ | id | first_name | last_name | email | +----+------------+-----------+---------------------+ | 2 | John | Rambo | [email protected] | | 4 | John | Carter | [email protected] | +----+------------+-----------+---------------------+