Check if MySQL query result is Empty PHP
- Article
- Comment
Check if PHP MySQL query result is Empty. Sometimes, we need to check whether the data is existing or not before inserting a new one. Basically, the operations needs to query and get the results count. There is no other options now. So just use the following code to get the results.
<?php $sql = mysql_query("SELECT * FROM table"); if(mysql_num_rows($sql) > 0){ $final_query = "UPDATE Kv_table_name SET user_id=".$user_id." user_name=".$user_name." WHERE id=".$id ; } else{ $final_query = "INSERT INTO Kv_table_name (user_id, user_name) VALUES("234" ,"Kvvaradha"); } $result = mysql_query($final_query); ?>
Here the above code tests, whether the result exist or not, than it will insert a new value or update existing one with new values,