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


mysql_c_api.c - source /* Mike Massingill - MikeM@OuSob.Com db_mysql.h - http://www.OuSob.com 1 g++ -Wall mysql_c_api.c -I/usr/include /usr/lib64/mysql/libmysqlclient.a -O3 -lz -L. -o mysql_c_api 2 chmod +x mysql_c_api 3 Syntax: ./mysql_c_api <database> <filename_containing_query> [HTML,XML] */ #include "db_mysql.h" //----------------------------------------------------------------------------------------------------------- main int main (int argc, char *argv[], char *env[]) { FILE *stream; char query[8192]; char argv_database[128]; char argv_file[128]; char fld_name[128]; char fld_value[256]; int html_flag = 0; int xml_flag = 0; int j = 0; int num_rows = 0; unsigned int num_fields = 0; int loop_ct = 0; int row_ct = -1; char *bgcolor_light = "#ffffff"; char *bgcolor_dark = "#f9f9f9"; char *web; DB *db; db = new DB; // make sure no one calls this directly from web server web = getenv("REQUEST_METHOD"); if( web ){ printf("Content-type: text/html \n\n"); printf("I don't think so!\n"); return 1; } // linux clear screen system("clear > nul"); // make sure argument count is right if( argc < 3 ){ show_how_stdin(argv[0]); return 1; } // see if HTML output parameter is sent if( (argc == 4 && (strcmp(argv[3],"HTML") == 0)) ){ html_flag = 1; } // see if XML output parameter is sent if( (argc == 4 && (strcmp(argv[3],"XML") == 0)) ){ xml_flag = 1; } // put argv[]s into memory variables sprintf(argv_database,"%s",argv[1]); sprintf(argv_file ,"%s",argv[2]); // open the file containing the query stream = fopen(argv_file,"r"); if( !stream ){ printf("\n\nFile (%s) not found\n\n",argv_file); return 1; } //read contents of query file fread(query,sizeof(char),8192, stream); fclose(stream); // go to work db->my_select_db(argv_database); if( db->error_flag ){ printf("\n\nError # %u %s\n\n",db->error_flag,db->error_message); db->my_disconnect(); return 1; } db->my_query(query); if( db->error_flag ){ printf("\n\nError # %u %s\n\n",db->error_flag,db->error_message); db->my_disconnect(); return 1; } db->my_store_result(); if( db->error_flag ){ printf("\n\nError # %u %s\n\n",db->error_flag,db->error_message); db->my_disconnect(); return 1; } if( !db->my_num_rows() ){ printf("\n\nError # %u %s\n\n",db->error_flag,db->error_message); db->my_disconnect(); return 1; } num_rows = db->my_num_rows(); if( html_flag ){ // ---------------------------------------------------------------------------------- HTML printf("<html>\n"); printf("<head>\n"); printf("<base href=\"http://www.ousob.com/\">\n"); printf("<title>OuSob - MySql C API Example</title>\n"); printf("<meta name=\"author\" content=\"Mike Massingill - MikeM@OuSob.com\">\n"); printf("<meta name=\"keywords\" content=\"MySql C API Example,g++,Linux,SUSE,Mike Massingill,OuSob\">\n"); printf("<meta name=\"description\" content=\"MySql C API Example - Outputs text,HTML, and XML\">\n"); printf("<style>\n"); printf(" table.soap { border-color: #600; border-width: 0 0 1px 1px;border-style: solid; }\n"); printf(" td.soap { border-color: #600; border-width: 1px 1px 0 0; border-style: solid; margin: 0; padding: 1px; background-color: #ffe; }\n"); printf("</style>\n"); printf("</head>\n"); printf("<body topmargin=\"0\">\n"); printf("<table class=\"soap\" cellpadding=\"2\" cellspacing=\"0\" border=\"1px\">\n"); while( db->my_fetch_row() ){ if( loop_ct == 0 ){ // OuSob image printf(" <tr>\n"); printf(" <td nowrap align=\"left\" colspan=\"%i\" bgcolor=\"#ffffff\"><a href=\"http://www.ousob.com/\"><img alt=\"http://www.ousob.com --- Legacy Redefined\" border=\"0\" src=\"http://www.ousob.com/sb_icons/cxutil.jpg\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"blue\">mysql_c_api.c</font> written by Mike Massingill - MikeM@OuSob.Com</td>\n",db->my_num_fields()); printf(" </tr>\n"); // query printf(" <tr>\n"); printf(" <td class=\"soap\" nowrap align=\"left\" colspan=\"%i\"><pre><b>Query in <font color=\"blue\">%s</font></b><br>%s</pre></td>\n",db->my_num_fields(),argv_file,query); printf(" </tr>\n"); // row(s) printf(" <tr>\n"); printf(" <td class=\"soap\" nowrap align=\"left\" colspan=\"%i\">Row(s): %u</td>\n",db->my_num_fields(),num_rows); printf(" </tr>\n"); // date printf(" <tr>\n"); printf(" <td class=\"soap\" nowrap align=\"left\" colspan=\"%i\">Date: %s</td>\n",db->my_num_fields(),db->date_str); printf(" </tr>\n"); // time printf(" <tr>\n"); printf(" <td class=\"soap\" nowrap align=\"left\" colspan=\"%i\">Time: %s</td>\n",db->my_num_fields(),db->time_str); printf(" </tr>\n"); // column headers printf(" <tr>\n"); num_fields = db->my_num_fields(); //----------------------------- for( j=0; (unsigned int)j<num_fields; j++){ sprintf(fld_name,"%s",db->my_field_name(j)); printf(" <td class=\"soap\" nowrap>%s</td>\n",fld_name); } //----------------------------- printf(" </tr>\n"); loop_ct = 1; } if( (++row_ct % 2) == 0 ){ printf(" <tr bgcolor=\"%s\">\n",bgcolor_dark); }else{ printf(" <tr bgcolor=\"%s\">\n",bgcolor_light); } // field values ----------------- for( j=0; (unsigned int)j<num_fields; j++){ sprintf(fld_name,"%s",db->my_field_name(j)); sprintf(fld_value,"%s",db->value_of(fld_name)); printf(" <td class=\"soap\" nowrap>%s</td>\n",fld_value); } printf(" </tr>\n"); } printf("</table>\n"); printf("<br>\n"); printf("\nProcessing Time %s second(s)\n",db->my_difftime()); printf("</body>\n"); printf("</html>\n"); } if( xml_flag ){ // ---------------------------------------------------------------------------------- XML printf("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n"); printf("<root>\n"); printf(" <author>Mike Massingill - MikeM@OuSob.Com</author>\n"); printf(" <query_file>%s</query_file>\n",argv_file); printf(" <query>%s</query>\n",query); printf(" <rows>%u</rows>\n",num_rows); printf(" <date>%s</date>\n",db->date_str); printf(" <time>%s</time>\n",db->time_str); while( db->my_fetch_row() ){ printf(" <row>\n"); num_fields = db->my_num_fields(); for( j=0; (unsigned int)j<num_fields; j++){ sprintf(fld_name,"%s",db->my_field_name(j)); sprintf(fld_value,"%s",db->value_of(fld_name)); printf(" <%s>%s</%s>\n",fld_name,fld_value,fld_name); } printf(" </row>\n"); } printf(" <processing_time>%s second(s)</processing_time>\n",db->my_difftime()); printf("</root>\n"); } if( !xml_flag && !html_flag ){ // -------------------------------------------------------- NORMAL OUTPUT printf("Author: Mike Massingill - MikeM@OuSob.Com\nQuery in %s\n%s\n\nRow(s): %ld\nDate: %s\nTime: %s\n\n",argv_file,query,db->my_num_rows(),db->date_str,db->time_str); while( db->my_fetch_row() ){ num_fields = db->my_num_fields(); for( j=0; (unsigned int)j<num_fields; j++){ sprintf(fld_name,"%s",db->my_field_name(j)); sprintf(fld_value,"%s",db->value_of(fld_name)); printf("%2i %-40s = %s\n",j,fld_name,fld_value); } printf("\n"); } printf("\nProcessing Time %s second(s)\n",db->my_difftime()); } db->my_free_results(); db->my_disconnect(); return 0; }