coder9 Posted July 25, 2008 Share Posted July 25, 2008 ok i have this table name: jtable with two columns: id and box_no now what i want is to check first if $link has the same value already in box_no column. I want the search to search all the records in the table if $link has duplicate already under box_no column if duplicate found, then don't write $link value into box_no column. if it doesn't have duplicate, then write the value of $link into box_no column. now my humble request is this could you show me how is this done using PHP & MySQL? Thank you very much in advance. Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/ Share on other sites More sharing options...
samshel Posted July 25, 2008 Share Posted July 25, 2008 sudo code... $strQuery1 = "select count(*) as cnt from jtable where box_no='".$link."'"; $res = mysql_query($strQuery1); $row = mysql_fetch_array($res); $intCount = $row['cnt']; if($intCount == 0) { $strQuery2 = "insert into jtable (box_no) values ('".$link."')";//assumption id is auto-incremented. $res2 = mysql_query($strQuery2); } Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/#findComment-599294 Share on other sites More sharing options...
waynew Posted July 25, 2008 Share Posted July 25, 2008 Or $amount = mysql_num_rows($result); Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/#findComment-599303 Share on other sites More sharing options...
samshel Posted July 25, 2008 Share Posted July 25, 2008 i prefer count() as it does not actually fetch all rows but just returns count() of rows..in case the table has a lot of records...it will fetch all rows when we just need a count of rows and not the actual data. Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/#findComment-599305 Share on other sites More sharing options...
waynew Posted July 25, 2008 Share Posted July 25, 2008 Lesson learned. Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/#findComment-599308 Share on other sites More sharing options...
coder9 Posted July 25, 2008 Author Share Posted July 25, 2008 @samshel Thank you very much man! You really helped me. I'll pray for you. Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/#findComment-599319 Share on other sites More sharing options...
ignace Posted July 25, 2008 Share Posted July 25, 2008 why not just use: REPLACE INTO? Link to comment https://forums.phpfreaks.com/topic/116549-ho-to-check-record-existence-and-write-if-not-exist/#findComment-599321 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.