Checking if data exists in database

mysql, optimization, php

Solution

Let the database count and retrieve the count data from the query.

$result = mysql_query('SELECT COUNT(*) FROM `table` WHERE `field` = ...');
if (!$result) {
    die(mysql_error());
}
if (mysql_result($result, 0, 0) > 0) {
    // some data matched
} else {
    // no data matched
}

Problem

What is the right way of checking if the required data is in the database? What I use currently is, `mysql_query("SELECT anyfield FROM table WHERE field='$data'");` and then check the if any rows are affected. But I dont really have any use with the extracted data `anyfield`. Eventhough the resource usage is so minor here, what is the right way to check if data exists in a db without extracting any other fields from the table?

Original source