sonu 0 Posted July 14, 2012 Report Share Posted July 14, 2012 Hi Guys! I have 2 fields name & email in mysql database.I want to check if the record exists or not in the DB and if not then it would insert records in the database. Quick help really appreciated. Thanks! Quote Link to post Share on other sites
birbal 168 Posted July 14, 2012 Report Share Posted July 14, 2012 does your field have any unique column or primary key column? Quote Link to post Share on other sites
php_developer 0 Posted July 14, 2012 Report Share Posted July 14, 2012 $name;$email;$query = mysql_query("select * from table_name where name=$name and email=$email");$num_of_row=mysql_num_rows($query);if($num_of_row <= 0){$another_query=mysql_query("insert into table_name (name, email) values ($name, $email)");} Quote Link to post Share on other sites
birbal 168 Posted July 14, 2012 Report Share Posted July 14, 2012 (edited) You dont need to send a query(reduce overhead) if you have any PK or unique constraint in your database table. if your database schema wants any unique column you should constraint it from the database. you can directly execute the INSERT, if there is no any duplication it will get inserted. otherwise when it fails it sends error, you check the specific error number using mysql_errno() and you can show any specific custom error on that. you can check list of mysql error number here ==> http://dev.mysql.com...ges-server.htmlhttp://php.net/mysql_errno Edited July 14, 2012 by birbal Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.