How to write PHP script to fetch data, based on some conditions, from MySQL table?

To fetch data from a MySQL table based on specific conditions, you use the WHERE clause in your SQL statement combined with PHP database functions. The WHERE clause filters records that meet specified criteria, allowing you to retrieve only the data you need.

Using MySQLi (Recommended)

Here's a modern approach using MySQLi to fetch records from a table where the author name is 'Sanjay' −

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "TUTORIALS";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL query with WHERE clause
$sql = "SELECT tutorial_id, tutorial_title, tutorial_author, submission_date 
        FROM tutorials_tbl 
        WHERE tutorial_author = 'Sanjay'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Fetch and display data
    while($row = $result->fetch_assoc()) {
        echo "Tutorial ID: " . $row["tutorial_id"] . "<br>";
        echo "Title: " . $row["tutorial_title"] . "<br>";
        echo "Author: " . $row["tutorial_author"] . "<br>";
        echo "Submission Date: " . $row["submission_date"] . "<br>";
        echo "--------------------------------<br>";
    }
} else {
    echo "No records found";
}

$conn->close();
?>

Using Prepared Statements

For better security against SQL injection, use prepared statements −

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "TUTORIALS";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Prepare statement
$author_name = "Sanjay";
$stmt = $conn->prepare("SELECT tutorial_id, tutorial_title, tutorial_author, submission_date FROM tutorials_tbl WHERE tutorial_author = ?");
$stmt->bind_param("s", $author_name);
$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    echo "Tutorial ID: " . $row["tutorial_id"] . "<br>";
    echo "Title: " . $row["tutorial_title"] . "<br>";
    echo "Author: " . $row["tutorial_author"] . "<br>";
    echo "Submission Date: " . $row["submission_date"] . "<br>";
    echo "--------------------------------<br>";
}

$stmt->close();
$conn->close();
?>

Common WHERE Conditions

Condition SQL Example Description
Equality WHERE author = 'Sanjay' Exact match
Pattern matching WHERE title LIKE '%PHP%' Contains 'PHP'
Range WHERE id BETWEEN 1 AND 10 Value range
Multiple conditions WHERE author = 'Sanjay' AND status = 'active' AND/OR operators

Conclusion

Use MySQLi or PDO instead of deprecated mysql functions for better security and performance. Always use prepared statements when dealing with user input to prevent SQL injection attacks.

Updated on: 2026-03-15T07:24:20+05:30

807 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements