Check if MySQL query result is Empty PHP
X

Congrats, You are Subscribed to Receive Updates.

Check if MySQL query result is Empty PHP


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,

 

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts