Search code examples
mysqlcstringstrcmp

Strcmp not with working data in mysql


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql.h>
#include <my_global.h>

void replace(char * o_string, char * s_string, char * r_string) {
      char buffer[1024];
      char * ch;
      if(!(ch = strstr(o_string, s_string)))
              return;
    strncpy(buffer, o_string, ch-o_string);
      buffer[ch-o_string] = 0;
    sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
      o_string[0] = 0;
      strcpy(o_string, buffer);
      return replace(o_string, s_string, r_string);
 }

int main()
{
    MYSQL *pConn;
    MYSQL_RES *pRes;
    MYSQL_ROW aRow;
    MYSQL_FIELD *field;
    int nfields, iCounter;
    pConn = mysql_init(NULL);
    char aPassword[1024]="";    
    if (pConn == NULL){
            printf("Error %u: %s\n", mysql_errno(pConn), mysql_error(pConn));
              exit(1);
        }
    if(mysql_real_connect(pConn, 0, "root",aPassword,"data",0,NULL,0) == NULL){
        printf("Error %u: %s\n", mysql_errno(pConn), mysql_error(pConn));
             exit(1);
        }       
    char *info;
    printf("Content-type:text/html\n\n");
    printf("<html><body>"); 
    info = getenv("QUERY_STRING");
    char sub[20]="";
    int nsub;
    char teacher[20]="";
    char room[20]="";
    int nroom;
    int count=0;    
    char *token;
    char arr[5][20];
    char data[1024];
    char aCommand[1024];
    char data2[1024];
    replace(info,"%20"," ");
    replace(info,"name=","");
    token = strtok(info, " ");
    while(token!=NULL){
        strcpy(arr[count],token);   
        count++;
        token = strtok(NULL," ");
    }
    if(count==5){
        strcat(arr[0]," ");     
        strcat(arr[0],arr[1]);
        strcpy(sub,arr[0]);
        strcpy(teacher,arr[2]);
        strcat(arr[3]," ");     
        strcat(arr[3],arr[4]);
        strcpy(room,arr[3]);
    }
    if(count==4){
        strcat(arr[0]," ");     
        strcat(arr[0],arr[1]);
        strcpy(sub,arr[0]);
        strcpy(teacher,arr[2]);
        strcpy(room,arr[3]);

    }
    strcat(teacher," ");
    strcat(teacher,room);
    strcat(sub," ");
    strcat(sub,teacher);
    puts(sub);
    sprintf(aCommand,"select * from Schedule");
    mysql_query(pConn, aCommand);
    pRes = mysql_store_result(pConn);
    nfields = mysql_num_fields(pRes);   
    while ((aRow = mysql_fetch_row(pRes))){
        if(strncmp(aRow[2],sub,strlen(sub)-1)==0)
            puts("YS");
        else
            puts("NO");
    }   

    mysql_free_result(pRes);
    mysql_close(pConn);
    printf("</body></html>");
    return 0;
}

That's the whole code. I don't know where the problem is. After I parsed the query string and display them, it seems that the field and the parsed string are equal. But I've been trying to use strcmp and they just won't work. What could be the error?


Solution

  • I found the problem. I tried using strstr to find the string that only matches with the data in the MySQL table. It doesn't match the whole string so I just modified the program. Instead of matching the whole string, I just used a part of the string to match with the data in the table.