Categories
DATABASE Programming/misc Programming/php

mysql c api 設定執行時的 timeout 時間

int fnSetTimeout(int timeout) {
  char **row;
  char sql[100];
  char data[200];

  sprintf(sql,"set session wait_timeout=%d",timeout);
  if ( mysql_real_query( &mysql, sql, strlen(sql) ) ) {
    printf("FAILED : %s\n",sql);
    exit(3);
  }

  sprintf(sql,"set session interactive_timeout=%d",timeout);
  if ( mysql_real_query( &mysql, sql, strlen(sql) ) ) {
    printf("FAILED : %s\n",sql);
    exit(3);
  }

  strcpy(sql,"select @@session.interactive_timeout" );
  if ( mysql_real_query( &mysql, sql, strlen(sql) ) ) {
    printf("FAILED : %s\n",sql);
    exit(3);
  }

  if ( (result=mysql_store_result(&mysql)) != NULL ) {
    row=mysql_fetch_row(result);
    strcpy( data, row[0] );
    mysql_free_result( result );
    timeout=atoi(data);
  } else {
    timeout=0;
  }

  return(timeout);
}