Jump to content

ifelse By Row Count Not Working


TecTao

Recommended Posts

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.

Link to comment
Share on other sites

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')
Link to comment
Share on other sites

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";
}
 
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.