Pasang libmysql++ dengan cara
sudo apt-get install libmysql++ libmysql++-dev
tambahkan kode berikut pada file test.cpp
#include <iostream> #include <vector> #include <string> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cgicc/CgiDefs.h> #include <cgicc/Cgicc.h> #include <cgicc/HTTPHTMLHeader.h> #include <cgicc/HTMLClasses.h> #include <mysql.h> using namespace std; using namespace cgicc; #define SERVER "localhost" #define USER "root" #define PASSWORD "winda1984" #define DATABASE "user" int main() { Cgicc formData; cout << "Content-type:text/html\r\n\r\n"; cout << "<html>\n"; cout << "<head>\n"; cout << "<title>Using GET Methods</title>\n"; cout << "</head>\n"; cout << "<body>\n"; form_iterator t = formData.getElement("t"); if( !t->isEmpty() && t != (*formData).end()) { cout << "Table: " << **t << endl; MYSQL *connect; connect=mysql_init(NULL); if (!connect) { cout<<"Koneksi MySQL gagal"; return 1; } connect=mysql_real_connect (connect, "localhost", "user", "password" , "database", 0, NULL, 0); if (connect) { cout<<"connection Succeeded\n"; }else{ cout<<"connection failed\n"; } MYSQL_RES *res_set; MYSQL_ROW row; string str = "select * from " + **t; mysql_query (connect,str.data()); unsigned int i =0; res_set = mysql_store_result(connect); unsigned int numrows = mysql_num_rows(res_set); while (((row= mysql_fetch_row(res_set)) !=NULL )) { cout << "| \t" << row[i] << "\t |"<< endl; } mysql_close (connect); } else { cout << "No text entered for table" << endl; } cout << "<br/>\n"; cout << "</body>\n"; cout << "</html>\n"; return 0; }
compile dengan perintah
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -lcgicc -I/usr/include/mysql
sumber: https://sandeepghai.wordpress.com/2011/08/07/linking-of-mysql-database-with-c-on-linux-machine/