The MySQL WHERE clause is useful to filter only those records that are fulfilled by the user’s specific condition. The SQL WHERE Clause is useful to restrict the number of rows affected by a SELECT, UPDATE, or DELETE query.
Index
The WHERE clause works like an if condition in any programming language. The WHERE clause is useful to compare the given value with the field value available in a MySQL table. If the given value from outside is equal to the available field value in the MySQL table, it returns that row.
Syntax
SELECT Column1 , Column2 , ... FROM Table_Name WHERE Condition
Read Also: PHP MySQL Select Data
<?php $conn = mysqli_connect("localhost", "user_name", "password", "database"); if($conn === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $query = "SELECT * FROM Data WHERE Firstname='ram'"; if($result = mysqli_query($conn, $query)){ if(mysqli_num_rows($result) > 0){ echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_free_result($result); } else{ echo "No Matching records are found."; } } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn); } mysqli_close($conn); ?>
<?php $mysqli = new mysqli("localhost", "user_name", "password", "database"); if($mysqli === false){ die("ERROR: Could not connect. " . $mysqli->connect_error); } $query = "SELECT * FROM Data WHERE Firstname='ram'"; if($result = $mysqli->query($query)){ if($result->num_rows > 0){ echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while($row = $result->fetch_array()){ echo "<tr>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "</tr>"; } echo "</table>"; $result->free(); } else{ echo "No matching records are found."; } } else{ echo "ERROR: Could not able to execute $query. " . $mysqli->error; } $mysqli->close(); ?>
<?php try{ $pdo = new PDO("mysql:host=localhost; dbname=database", "user_name", "password"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { die("ERROR: Could not connect. " . $e->getMessage()); } try{ $query = "SELECT * FROM Data WHERE Firstname='ram'"; $result = $pdo->query($query); if($result->rowCount() > 0){ echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while($row = $res->fetch()){ echo "<tr>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "</tr>"; } echo "</table>"; unset($res); } else{ echo "No records matching are found."; } } catch(PDOException $e) { die("ERROR: Could not able to execute $sql. " . $e->getMessage()); } unset($pdo); ?>
The WHERE clause is useful to fetch the selected rows according to the specifications mentioned by the user. It is a useful tool to efficiently work with databases.
In case, you want to learn about how to connect to MySQL using PHP and creating databases, do check out our previous blogs. Happy Learning 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…