Hi guys,

need ur help regarding the above matter. I've write a code for if statement but it didnt work as expected when I run the program. Below is my code:

// Get ChannelID 
strcpy(src, row[11]);
if (strcmp (src, "1") == 0)
{
  if (sprintf(query2, "select * from Register where MasterAccNo = 'NONE'") == 0);
  {
    strcpy (src, "2P_SMS_RegisteredXPax");
    strcpy(src2, src);
    printf ("%s\n",src2);

    sprintf(string2,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|||||%s|", 
    row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, src2);
    sprintf(mobileno,"%s", row[0]);
    printf ("%s\n",string2);

     writeLogFile(string2, mobileno);
     //i++;
    }

    if (sprintf(query3, "select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE'") == 0);
    {
      strcpy (src, "2P_SMS_PostpaidAccount");
      strcpy(src3, src);
      printf ("%s\n",src3);

      sprintf(string3,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|||||%s|", 
      row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, src3);
      sprintf(mobileno,"%s", row[0]);
      printf ("%s\n",string3);

      writeLogFile(string3, mobileno);
      //i++;
        }
    //else 
    if (sprintf(query4, "select * from Register where substring(MasterNumber,2,1) <> '1' and MasterAccNo <> 'NONE'") == 0);
    {
      strcpy (src, "2P_SMS_TM");
      strcpy(src4, src);
      printf ("%s\n",src4);

      sprintf(string4,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|||||%s|", 
      row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, src4);
      sprintf(mobileno,"%s", row[0]);
      printf ("%s\n",string4);

      writeLogFile(string4, mobileno);
      //i++;
      }
     }

     i++;

When I run this program, it executes all three conditions. Supposedly, only 1 condition/query matched and will be executed and then write into a file but then, in this case, all three queries executed caused 3 same data written into a file.
The result written in the file when I run this program is:

0135767903|WONG HWA HUONG|49022811355468||4A, LRG MERDEKA 2,96000 SIBU|,|96000|MALAYSIA|30-11-2006|||||2P_SMS_RegisteredXPax|

0135767903|WONG HWA HUONG|49022811355468||4A, LRG MERDEKA 2,96000 SIBU|,|96000|MALAYSIA|30-11-2006|||||2P_SMS_PostpaidAccount|

0135767903|WONG HWA HUONG|49022811355468||4A, LRG MERDEKA 2,96000 SIBU|,|96000|MALAYSIA|30-11-2006|||||2P_SMS_TM|

supposedly this account will be 2P_SMS_TM

Any help?

Many thanks

need ur help regarding the above matter.

Please state the matter in the post. Don't use the title as part of your question. It's only meant to give us an idea about what we're about to read.

I've write a code for if statement but it didnt work as expected when I run the program.

I assume you mean the IF statments like:

if (sprintf(query2, 
            "select * from Register where MasterAccNo = 'NONE'") 
                    == 0)[b];[/b]

What the sprintf() statement does is load the first parameter (query2) with the rest of the parameters and returns the number of characters loaded. Therefore, each and every IF will fail.

But notice the character at the end of the IF (in red), that will end the IF. Therefore the code block is executed because it's not part of the IF at all.

Hi WaltP,

Thanks for ur reply. I get what do u mean for this statement:

But notice the character at the end of the IF (in red), that will end the IF. Therefore the code block is executed because it's not part of the IF at all.

but for this statement:

What the sprintf() statement does is load the first parameter (query2) with the rest of the parameters and returns the number of characters loaded. Therefore, each and every IF will fail.

how should i modify my IF statement in order for the query to execute properly based on the conditions provided?..

Many thanks

What he means is that this piece of code:

if (sprintf(query3, "select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE'") == 0)

Will always be false. Sprintf will return the value of chars loaded into query3.
So in this case it would return 89 because select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE' is 89 chars (including spaces) long.

how should i modify my IF statement in order for the query to execute properly based on the conditions provided?..

You are using the wrong process.
1) Load the query string into the variable (the sprintf() part)
2) Execute the query using some database function. This you didn't do.
3) Now check the return from the database call with the IF

Hi Guys...

Thanks for ur help..my problem already solved..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.