Where cluase is used to only fetch those records that full fill a specified records
This is our where clause query with select statement
SELECT * FROM users WHERE name =”rahul” . Now this query will give all
record of rahul
Now this is our table in database and the database is test and the table name is users .
id | name | age | |
1 | ronit Singh | 34 | raja@gmail.com |
2 | ankit | 33 | ankit@gmail.com |
3 | rahul | 11 | raul@gmail.com |
4 | sharad | 31 | shar@gmail.com |
5 | raja | 31 | raja123@gmail.com |
Now i will write php code to show this table on browser with help of where clause query . As you can see there is one raja in the table so i will try to fetch it by using php-mysql .Now below is our php code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php $conn = mysqli_connect("localhost", "root", "", "test"); if($conn === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } // This query will fetach our data from db $sql = "SELECT * FROM users WHERE name='Raja'"; if($result = mysqli_query($conn, $sql)) { if(mysqli_num_rows($result) > 0) { echo "<table>"; echo "<tr>"; echo "<th>id</th>"; echo "<th>Name</th>"; echo "<th>Age</th>"; echo "<th>Email</th>"; echo "</tr>"; while($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } echo "</table>"; // Close result set mysqli_free_result($result); } else{ echo "<font color ='red'>Sorry no matching found try again.</font>"; } } else{ echo "ERROR: Cant not execute $sql. " . mysqli_error($conn); } // Close connection mysqli_close($conn); ?> |
This where clause query using php code and below is our output
id | name | age | |
5 | raja | 31 | raja123@gmail.com |
Nice Tutorial Bro
[…] Where clause in php-mysql PHP – PDO Insert data into database using OOP Concept […]