How to Check if value exists in a MySQL database

后端 未结 6 703
眼角桃花
眼角桃花 2020-12-12 17:15

Suppose I have this table:

id | name | city
------------------
1  | n1   | c1
2  | n2   | c2
3  | n3   | c3
4  | n4   | c4

I want to check

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 18:12

    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.

提交回复
热议问题