php - How to Check if value exists in a MySQL database -


suppose have table:

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

i want check if value c7 exists under variable city or not.

if answer 'yes' something.
if answer 'no', else.

preferred way, using mysqli extension:

$mysqli = new mysqli(server, dbuser, dbpass, database); $result = $mysqli->query("select id mytable city = 'c7'"); if($result->num_rows == 0) {      // row not found, stuff... } else {     // other stuff... } $mysqli->close(); 

deprecated:

$result = mysql_query("select id mytable city = 'c7'"); if(mysql_num_rows($result) == 0) {      // row not found, stuff... } else {     // other stuff... } 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -