I am using mysql_fetch_array to fetch result from a table of my database and I want to exclude results from particular users from being fetch together with others.
Example:

$result = mysql_query("SELECT * FROM users") 
                or die(mysql_error());  
                
        
        
        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Sex</th> <th>Username</th></tr>";

        while($row = mysql_fetch_array( $result )) {
                
                // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['firstname'] . '</td>';
                echo '<td>' . $row['lastname'] . '</td>';
                echo '<td>' . $row['sex'] . '</td>';
                echo '<td>' . $row['username'] . '</td>';
                echo "</tr>"; 
        } 

        echo "</table>";

Example Results
ID First Name Last Name Sex Username
1 Adams John Male adamsjo
2 Jen Jensen Female jensenjen
3 Mark Mike Male miker
4 Mathew Denson Male denis
etc

How can i exclude results from the first two rows of ID 1 and 2 from showing together with the rest of the result?

Dani AI

Generated

Quick clarification and practical next steps (builds on the answers from , and @Ardav):

The important distinction is whether you want to exclude specific user IDs (explicitly remove users 1 and 2), or skip the first N rows in the result set (position-based). Without an ORDER BY the “first two rows” are undefined, so always define the ordering you expect.

Safer, production-ready approach (exclude specific IDs)

  • Move away from the old mysql_* calls (removed in modern PHP). Use PDO or mysqli and prepared statements to avoid SQL injection.
  • Build the exclusion list as an array and create placeholders at runtime so the query remains parameterized.

Example (PDO, dynamic placeholders and output escaping):

$exclude = [1,2];                       // validated ints
$ph = implode(',', array_fill(0, count($exclude), '?'));
$sql = "SELECT id, firstname, lastname, sex, username FROM users WHERE id NOT IN ($ph) ORDER BY id ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute($exclude);
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<tr><td>' . htmlspecialchars($r['id'], ENT_QUOTES) . '</td>...</tr>';
}

Skip first N rows (position-based)

  • Use ORDER BY + LIMIT/OFFSET to skip rows reliably. Example: ORDER BY id ASC LIMIT :offset, :count (bind as integers or cast to int before concatenating).

Extra tips

  • Prefer selecting explicit columns instead of SELECT *.
  • If excluding a very large list, store exclusions in a small table and LEFT JOIN to filter (better optimizer behavior).
  • Always validate that excluded values are integers and escape HTML output with htmlspecialchars to prevent XSS.

This gives a safe, maintainable path whether you mean “exclude IDs 1 and 2” (explicit) or “skip the first two returned rows” (positional).

Recommended Answers

All 3 Replies

Member Avatar for Member #120589
'SELECT * FROM users where id > 2'

Wow! I just used this query and it works:

$result = mysql_query("SELECT * FROM users WHERE id NOT IN ('1', '2')") 
                or die(mysql_error());

@Ardav, thanks so much, that also work well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.