Canman2005 Posted May 19, 2007 Share Posted May 19, 2007 Hi all I am trying to run the following query IF EXISTS(SELECT * FROM `md` WHERE `rn` = 'DD') UPDATE `md` SET `rn`='CC' ELSE INSERT `md` SET `rn`='BB'; But I get a #1064 - You have an error in your SQL syntax; Does anyone know why? Thanks Dave Link to comment https://forums.phpfreaks.com/topic/52115-if-exists-error/ Share on other sites More sharing options...
MadTechie Posted May 19, 2007 Share Posted May 19, 2007 Thats not a valid SQL statement.. infact what are you traying to do ? Link to comment https://forums.phpfreaks.com/topic/52115-if-exists-error/#findComment-257001 Share on other sites More sharing options...
Canman2005 Posted May 19, 2007 Author Share Posted May 19, 2007 im trying to see if a record exists and if it does to update the information and if it doesnt exist then it would insert the data Link to comment https://forums.phpfreaks.com/topic/52115-if-exists-error/#findComment-257015 Share on other sites More sharing options...
MadTechie Posted May 19, 2007 Share Posted May 19, 2007 your need to do the select then check mysql_num_rows($result); for the number of records found then if more than one apply update.. i think some research may help Link to comment https://forums.phpfreaks.com/topic/52115-if-exists-error/#findComment-257026 Share on other sites More sharing options...
pocobueno1388 Posted May 19, 2007 Share Posted May 19, 2007 <?php $sql = mysql_query("SELECT * FROM `md` WHERE `rn` = 'DD'"); $count = mysql_num_rows($sql); if ($count > 0){ //record exists mysql_query("UPDATE `md` SET `rn`='CC'"); } else { //record doesn't exist //INSERT QUERY HERE } ?> Your INSERT query doesn't make any sense...so you might want to do a google on that. Link to comment https://forums.phpfreaks.com/topic/52115-if-exists-error/#findComment-257047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.