TecTao Posted November 3, 2014 Share Posted November 3, 2014 I am looking to identify a particular company records from a table that holds multiple records of the same company but with different criteria such as source of payment and row count to delete a specific record from two table and update a third. The ifelse statememts don't working. There are 4 conditions. First is if the paid source = "MT1000". Second is if the paid source = "2411". The third condition is if the Row Count >= 2, and the forth condition is that the Row Count == 1. Even though on a particular query the row count is 2, the condition returned is Row Count = 1. If I remove one record and switch the order of the last two conditions, the row count is 1 but returns the Row Count <= 2. $result = mysql_query( "SELECT COUNT(1) FROM townSponsor_OBC_tools WHERE ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'" ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_result($result, 0, 0); echo 'Row Count: = '; echo $num_rows; if ( $ts_obc_paidSourcee == 'MTM1000' ) { echo "This Paid source is MTM1000 and Nothing is done"; } elseif ( $ts_obc_paidSourcee == '2411' ) { echo "This Paid source is 2411 and Nothing is done"; } elseif ( $num_rows == 1 || $ts_obc_paidSourcee <> 'MTM1000' || $ts_obc_paidSourcee <> '2411') { echo "Row Count is = 1 and paid source isn not MTM1000 or 2411"; mysql_query("DELETE FROM townSponsor_OBC_tools WHERE ts_obc_m_orgIDSponsor = '$ts_obc_m_orgIDSponsor' AND ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"); mysql_query("DELETE FROM townSponsor_members WHERE tsm_m_username = '$ts_obc_username' AND tsm_m_userID = '$ts_obc_userID' AND tsm_assocID = '$ts_obc_m_orgIDSponsor'"); mysql_query("UPDATE users SET zabp_paid = 'NEW_S' WHERE username = '$ts_obc_username' AND id = '$ts_obc_userID'"); } elseif ( $num_rows >= 2 || $ts_obc_paidSourcee <> 'MTM1000' || $ts_obc_paidSourcee <> '2411') { echo "Row Count is >= 2 and paid source isn not MTM1000 or 2411"; mysql_query("DELETE FROM townSponsor_OBC_toolsTest WHERE ts_obc_m_orgIDSponsor = '$ts_obc_m_orgIDSponsor' AND ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"); mysql_query("DELETE FROM townSponsor_members WHERE tsm_m_username = '$ts_obc_username' AND tsm_m_userID = '$ts_obc_userID' AND tsm_assocID = '$ts_obc_m_orgIDSponsor'"); } Thank you in advance for any insights or suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/ Share on other sites More sharing options...
Barand Posted November 3, 2014 Share Posted November 3, 2014 Try using "AND" instead of "||" Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1495636 Share on other sites More sharing options...
TecTao Posted November 3, 2014 Author Share Posted November 3, 2014 Thanks. I did try And and OR in place of && and ||. I don't think that's the problem. The code is not recognizing the $num_row count and then applying the conditions of the elseif statements. Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1495640 Share on other sites More sharing options...
Psycho Posted November 3, 2014 Share Posted November 3, 2014 Code does not just arbitrarily skip a condition. It is doing exactly what you have programmed it to do based upon the results. So, something is not as you think it is. Either the results are not what you think they are or the results are matching a condition you didn't think they would. Just add some better debugging logic to see ALL the applicable values and also put statements within the if/else code blocks But, I do see one thing that doesn't make sense in your first query SELECT COUNT(1) FROM townSponsor_OBC_tools WHERE ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID' Why do you have the two conditions in the WHERE clause? Typically both the User ID and the Username would be unique. So, only one would be needed. Also, for the last two ifelse conditions, the conditions don't match what you apparently are trying to test for. That is why Barand stated to use AND. Here is one example: elseif ( $num_rows == 1 || $ts_obc_paidSourcee <> 'MTM1000' || $ts_obc_paidSourcee <> '2411') { echo "Row Count is = 1 and paid source isn not MTM1000 or 2411"; The echo statement states that this block of code would be run because the row count is equal to 1 AND the paid source is not a specific value. But, that is NOT what the condition is checking. The condition is checking if the row count is 1 OR the paid source is one of two other values. Based upon the echo the condition should be elseif ( $num_rows == 1 AND $ts_obc_paidSourcee <> 'MTM1000' AND $ts_obc_paidSourcee <> '2411')) Also, the last condition (based on the echo statement, should be elseif ( $num_rows >= 2 AND $ts_obc_paidSourcee <> 'MTM1000' AND $ts_obc_paidSourcee <> '2411') Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1495643 Share on other sites More sharing options...
Psycho Posted November 3, 2014 Share Posted November 3, 2014 Try this with some add'l debugging $query = "SELECT COUNT(1) FROM townSponsor_OBC_tools WHERE ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"; $result = mysql_query($query) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_result($result, 0, 0); echo "Row Count: {$num_rows}<br>\n"; echo "Paid Source: {$ts_obc_paidSourcee}<br>\n"; if ( $ts_obc_paidSourcee == 'MTM1000' ) { echo "This Paid source is MTM1000 and Nothing is done"; } elseif ( $ts_obc_paidSourcee == '2411' ) { echo "This Paid source is 2411 and Nothing is done"; } elseif ( $num_rows == 1 AND $ts_obc_paidSourcee <> 'MTM1000' AND $ts_obc_paidSourcee <> '2411')) { echo "Row Count is = 1 and paid source isn not MTM1000 or 2411"; mysql_query("DELETE FROM townSponsor_OBC_tools WHERE ts_obc_m_orgIDSponsor = '$ts_obc_m_orgIDSponsor' AND ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"); mysql_query("DELETE FROM townSponsor_members WHERE tsm_m_username = '$ts_obc_username' AND tsm_m_userID = '$ts_obc_userID' AND tsm_assocID = '$ts_obc_m_orgIDSponsor'"); mysql_query("UPDATE users SET zabp_paid = 'NEW_S' WHERE username = '$ts_obc_username' AND id = '$ts_obc_userID'"); } elseif ( $num_rows >= 2 AND $ts_obc_paidSourcee <> 'MTM1000' AND $ts_obc_paidSourcee <> '2411') { echo "Row Count is >= 2 and paid source isn not MTM1000 or 2411"; mysql_query("DELETE FROM townSponsor_OBC_toolsTest WHERE ts_obc_m_orgIDSponsor = '$ts_obc_m_orgIDSponsor' AND ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"); mysql_query("DELETE FROM townSponsor_members WHERE tsm_m_username = '$ts_obc_username' AND tsm_m_userID = '$ts_obc_userID' AND tsm_assocID = '$ts_obc_m_orgIDSponsor'"); } else { echo "Did not match any condition"; } Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1495645 Share on other sites More sharing options...
TecTao Posted November 4, 2014 Author Share Posted November 4, 2014 Thanks for your comments Psycho. I see now what you and Barand are saying. You first comment about the conditions no being arbitrary makes complete sense. Any suggestions on where I might start on debugging. I am weak on that and it would help in solving the problems I come with. Again, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1495692 Share on other sites More sharing options...
Psycho Posted November 4, 2014 Share Posted November 4, 2014 Any suggestions on where I might start on debugging. ??? Did you see my last post? Take a look at what I did. I echo the values that are used in all the if/ifelse conditions. Then, I added a final else condition with an echo statement. Now, you should be able to see what is printed to the page and understand exactly what the values are and confirm that the right conditions are being executed. Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1495693 Share on other sites More sharing options...
TecTao Posted November 19, 2014 Author Share Posted November 19, 2014 Yes I saw that and made complete sense. Didn't know if there were any other approaces such as error logs and the such. thanks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/292248-ifelse-by-row-count-not-working/#findComment-1497029 Share on other sites More sharing options...
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.