c语言连接mysql
#include <mysql/mysql.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){ MYSQL mysql; MYSQL_ROW row; MYSQL_RES *result; unsigned int num_fields; unsigned int i; mysql_init(&mysql); puts("Content-type: text/html\r\n\r\n"); if(!mysql_real_connect(&mysql, "127.0.0.1", "root", "","test",3306,NULL,0)) { printf("connection failed,%s\n",mysql_error(&mysql)); } mysql_query(&mysql,"SET NAMES utf8"); if(!mysql_query(&mysql,"SELECT * FROM test")) { result = mysql_store_result(&mysql); if(!result) { perror("result"); exit(1); } num_fields = mysql_num_fields(result); while(row = mysql_fetch_row(result)) { for(i=0; i<num_fields; i++) { printf("%s\t",row[i]); } printf("\n"); } mysql_free_result(result); } mysql_close(&mysql); printf("connection ok!");}