#include
#include
#include
#include
#include "include/mysql.h"
int main(int argc, char *argv[]){
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW row;
int res;
char db[20] = "cms_";
char *ids;
char *shell;
char flag;
if( argc!=3){
printf("\n---------------------------------Note:------------------------- \n");
printf("Author: baiyu929\n");//作者
printf("Date: 2013年7月23日\n");//日期
printf("----------------------------------------------------------------- \n\n");
//exit(1);
}
//连接初始化
if(!mysql_init(&my_connection)){
fprintf(stderr, "数据库初始化失败!\n");
return EXIT_FAILURE;
}
if (mysql_real_connect(&my_connection, "localhost", "root", "123456","html_db",0,NULL,0)){
printf("数据库连接成功!\n");
strcat(db, argv[1]);
mysql_select_db(&my_connection, db);
printf("Connection success\n");
mysql_query(&my_connection, "set names gbk");
res= mysql_query(&my_connection, argv[2]);
if(res){
printf("SELECT error:%s\n",mysql_error(&my_connection));
}else{
res_ptr= mysql_store_result(&my_connection);
if(res_ptr){
printf("Retrieved %lu Rows\n", (unsigned long)mysql_num_rows(res_ptr));
ids= malloc(1);
*ids = '\0';
while(row= mysql_fetch_row(res_ptr)){
printf("%s\n", row[5]);
ids= realloc(ids, strlen(ids)+ strlen(" ")+1);
strcat(ids, " ");
ids= realloc(ids, strlen(ids)+ strlen(row[0])+1);
strcat(ids, row[0]);
}
}
mysql_free_result(res_ptr);
}
mysql_close(&my_connection);
}
shell= malloc(100 + strlen(argv[1]) + strlen(ids));
sprintf(shell,"/usr/local/bin/php /www/titancms/cron/publish_%s.php -a%s", argv[1], ids);
free(ids);
printf("您要执行的是:%s\n",shell);
printf("请检查命令是否正确? (y/n)\n");
scanf("%s",&flag);
if(flag=='y'){
system(shell);
}
free(shell);
return EXIT_SUCCESS;
}