sonu Posted July 14, 2012 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! Link to comment Share on other sites More sharing options...
birbal Posted July 14, 2012 Share Posted July 14, 2012 does your field have any unique column or primary key column? Link to comment Share on other sites More sharing options...
php_developer Posted July 14, 2012 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)");} Link to comment Share on other sites More sharing options...
birbal Posted July 14, 2012 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now