MySql C API Example - Called from PHP Script http://www.ousob.com --- Legacy Redefined
Intro PHP Script Text Produced HTML Produced XML Produced Run Text Run HTML Run XML q.sql mysql_c_api.c db_mysql.h Download


db_mysql.h - source /* Mike Massingill - MikeM@OuSob.Com db_mysql.h - http://www.OuSob.com included in mysql_c_api.c */ #include <fstream> #include <iostream> #include <vector> #include <string> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #include <ctype.h> #include <unistd.h> #include <time.h> #include <sys/time.h> #include <sys/types.h> #include <sys/signal.h> #include <sys/errno.h> #include "/usr/include/mysql/mysql.h" using namespace std; class DB { public: time_t start; time_t end; char total_time[64]; char date_str [128]; char time_str [128]; struct tm *daynow; char timestring[128]; time_t thistime; char host_name[128]; char user_name[128]; char password[128]; char database[128]; char err[256]; int error_flag; char error_message[1024]; char *p; char query[1024]; MYSQL *conn; MYSQL_RES *rslt; MYSQL_ROW row; MYSQL_FIELD *field; unsigned long num_rows; unsigned int num_fields; DB(){ sprintf(this->host_name,"localhost"); sprintf(this->user_name,"xselect"); sprintf(this->password, "xselect"); this->error_flag = 0; memset(this->error_message,'\0',1024); memset(this->query,'\0',1024); time(&this->start); this->daynow = localtime(&this->start); strftime(this->date_str, 128, "%Y-%m-%d",this->daynow); strftime(this->time_str, 128, "%k:%M:%S",this->daynow); this->my_connect(); } ~DB(); void my_connect(void){ this->conn = mysql_init(NULL); if( !this->conn ){ this->error_flag = mysql_errno(this->conn); sprintf(this->error_message,"%s",mysql_error(this->conn)); } if( (mysql_real_connect(this->conn,this->host_name,this->user_name,this->password,this->database,0,NULL,0) == NULL) ){ this->error_flag = mysql_errno(this->conn); sprintf (this->error_message, "%s",mysql_error(this->conn)); } } void my_select_db(char *database) { if( mysql_select_db(this->conn,database) != 0 ){ this->error_flag = mysql_errno(this->conn); sprintf(this->error_message, "%s",mysql_error(this->conn)); } } void my_query(char *query) { if( mysql_query(this->conn,query) != 0 ){ this->error_flag = mysql_errno(this->conn); sprintf(this->error_message,"%s",mysql_error(this->conn)); } } void my_store_result(){ this->rslt = mysql_store_result(this->conn); if( !this->rslt ){ this->error_flag = mysql_errno(this->conn); sprintf(this->error_message, "%s",mysql_error(this->conn)); } } int my_fetch_row(void) { this->row = mysql_fetch_row(this->rslt); return( (this->row == NULL ? 0 : 1) ); } long my_num_rows(void) { this->num_rows = mysql_num_rows(this->rslt); return( this->num_rows ); } unsigned int my_num_fields(void) { this->num_fields = mysql_num_fields(this->rslt); return( this->num_fields ); } char *my_field_name(int num) { this->field = mysql_fetch_field_direct(this->rslt,num); return this->field->name; } char *field_value(int num) { this->field = mysql_fetch_field_direct(this->rslt,num); return this->row[num]; } char *value_of(char *field_name) { register int i = 0; int found = 0; int len = mysql_num_fields(this->rslt); for( i=0; i < len; i++ ){ this->field = mysql_fetch_field_direct(this->rslt,i); if( strcmp(this->field->name,field_name) == 0 ){ found = 1; break; } } if( found ){ return this->row[i]; }else{ sprintf(this->err,"Field %s not found in row!",field_name); return this->err; } } void my_free_results(void) { mysql_free_result(this->rslt); } void my_data_seek(unsigned int offset) { mysql_data_seek(this->rslt,offset); } void my_disconnect(void){ mysql_close(this->conn); } char *my_difftime(void){ time(&this->end); sprintf(this->total_time,"%f",difftime(this->end,this->start)); return this->total_time; } }; void show_how(char *prg) { printf("\n\nSyntax: %s <database> <database>\n\n",prg); } void show_how_stdin(char *prg) { printf("\n\nSyntax: %s <database> <filename_containing_query> [HTML,XML]\n\n",prg); }