How to Check if value exists in a MySQL database
preferred way, using MySQLi extension:
$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);
$result = $mysqli->query("SELECT id FROM mytable WHERE city = 'c7'");
if($result->num_rows == 0) {
// row not found, do stuff...
} else {
// do other stuff...
}
$mysqli->close();
deprecated:
$result = mysql_query("SELECT id FROM mytable WHERE city = 'c7'");
if(mysql_num_rows($result) == 0) {
// row not found, do stuff...
} else {
// do other stuff...
}
For Exact Match
"SELECT * FROM yourTable WHERE city = 'c7'"
For Pattern / Wildcard Search
"SELECT * FROM yourTable WHERE city LIKE '%c7%'"
Of course you can change '%c7%'
to '%c7'
or 'c7%'
depending on how you want to search it. For exact match, use first query example.
PHP
$result = mysql_query("SELECT * FROM yourTable WHERE city = 'c7'");
$matchFound = mysql_num_rows($result) > 0 ? 'yes' : 'no';
echo $matchFound;
You can also use if
condition there.