segmentation fault in mysql_query in C++
by rahulvishwakarma from LinuxQuestions.org on (#5BFN0)
hi to all, I am building a small project in MYSQL server and c++ as client in two VMs.
here is code where segmentation fault occures.( //segmentation error is commented out here)
Code:void classbilling::billing()
{
clrscr();
drawrect();
mysql = Conn::connection();
char yesno;
int lines;
int billno;
int amount;
int instock;
int numrowsaffected;
int stock;
std::string strquantity;
long memno;
long cardno;
long memcardno;
std::string strcardno;
std::string stramxid;
std::string strbillno;
std::string strmemno;
std::string strstock;
std::string strrate;
std::string strtotal;
std::string strmaxid;
float less = 1.0;
MYSQL_RES *res;
MYSQL_ROW row;
gotoxy(10, 6);
cout << "Are you a member : ";
cin >> yesno;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toupper(yesno) == 'Y')
{
gotoxy(10, 7);
cout << "Enter Membership Number : ";
std::string strmemno;
getline(cin, strmemno);
int m = Conn::intvalidity(strmemno);
memno = m;
if(m == 0)
{
draw->gotoxy(10,10);
cout << "Enter Valid Membership number";
cin.get();
return;
}
sql = "select name from tableMemberRecords where membershipno = '"+strmemno+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
gotoxy(10, 10);
cout << "Invalid Membership number";
cin.get();
}
}
}
gotoxy(10, 8);
cout << "Enter Card Number : ";
std::string strcardno;
getline(cin, strcardno);
m = Conn::intvalidity(strcardno);
if(m == 0)
{
draw->gotoxy(10,10);
cout << "Enter Valid card number";
cin.get();
return;
}
//display of member
gotoxy(1, 12);
cout << "before memsearch ";
cin.get();
mem1 = mem2->memberSearch(memno );
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
cardno = m;
if(mem1->getmemcardno() != cardno)
{
gotoxy(10, 9);
cout << "Incorrect card number";
cin.get();
return;
}
gotoxy(1, 14);
cout << "after if mem1";
cin.get();
//strmemno = std::to_string(memno);
cin.get();
cout << "after to_string strmemeno : " << strmemno;
cin.get();
// mysql_free_result(res);
sql = "select CardNo, AmountDeposited from tableMemberRecords where MembershipNo = '"+strmemno+"';";
cout << "after sql : " << sql;
cin.get();
qstate = mysql_query(mysql, sql.c_str()); //segmentation error
gotoxy(1, 15);
cout << "after sql";
cin.get();
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res))!= nullptr)
{
memcardno = atol(row[0]);
amount = atoi(row[1]);
// gotoxy(1, 14);
}
}
else
{
gotoxy(1, 18);
cout << "Error in else of select cardno : " << mysql_error(mysql);
cin.get();
}
if(cardno != memcardno)
{
gotoxy(10, 14);
cout << "incorrect card number or membership number";
cin.get();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
else if(mem1->getmemamount() > 100)
{
less = 0.95f;
amount -= 5;
mem1->setmemamount(amount);
}
else
{
gotoxy(10, 13);
cout<< "Amount less than 100 ";
gotoxy(10, 14);
cout << "Continue with normal billing ";
cin >> yesno;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toupper(yesno) == 'N')
{
return;
}
}
gotoxy(1, 14);
cout << "before to_string";
cin.get();
string stramt = std::to_string(amount);
strcardno = std::to_string(cardno);
strmemno = std::to_string(memno);
gotoxy(1, 15);
cout << "before update sql";
cin.get();
sql = "update tableMemberRecords set amountdeposited = '"+stramt+"' where membershipno = '"+strmemno+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(2, 16);
cout << "Member Record is updated and updated record is : ";
gotoxy(1, 17);
mem2->memberSearch(memno);//, 0, 15);
std::cin.get();
}
}
else
{
memno = 0;
}
sql = "select max(Billno) from tableBilling";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
billno = 0;
}
else
{
billno = atoi(row[0]);
}
strbillno = std::to_string(++billno);
}
}
product *prd = new product;
LABEL1:
clrscr();
drawrect();
sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
strbillno+"' and p.productname in (select productname from tableProductRecords where productid = b.productid);";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(1, 13);
res = mysql_store_result(mysql);
sqlprocess *sqlp = new sqlprocess;
numrowsaffected = sqlp->process_result_set(mysql, res);
}
productid = prd->displayProduct("PRODUCT DISPLAY");
if(productid == 0)
{
draw->gotoxy(10, 10);
cout << "Incorrect Product id";
cin.get();
// return;
goto LABEL1;
}
if(productid > 0)
{
clrscr();
drawrect();
getcmenu->printMenu(salesmenu, "Billing");
gotoxy(45, 3);
cout << "Bill No. : " << billno;
gotoxy(31, 5);
cout << productid;
string strpid = std::to_string(productid);
sql = "select productname, stock, rate from tableProductRecords where productid = '"+strpid+"';";
qstate = mysql_query(mysql,sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
gotoxy(31, 6);
productname = row[0];
cout << productname;
gotoxy(31, 7);
stock = atoi(row[1]);
cout << stock;
gotoxy(31, 8);
rate = atoi(row[2]);
cout << rate;
}
mysql_free_result(res);
}
sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
strbillno+"' and productname in (select productname from tableProductRecords where productid = b.productid);";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(1,13);
res = mysql_store_result(mysql);
sqlprocess *sqlp = new sqlprocess;
lines = sqlp->process_result_set(mysql, res);
}
do
{
gotoxy(31, 9);
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int x = 31; x <= w.ws_col-1; x++)
{
cout << " ";
}
gotoxy(31, 9);
getline(cin, strquantity);
quantity = Conn::intvalidity(strquantity);
if(quantity == 0)
{
gotoxy(10, 10);
cout << "Invalid Quantity";
cin.get();
gotoxy(31, 9);
}
}while(quantity == 0);
gotoxy(15, 10);
cout << "Total : ";
int qty = stock - quantity;
if(qty >= 0)
{
strqty = std::to_string(qty);
total = quantity * rate;
gtotal += total;
gotoxy(31, 10);
cout << total;
}
else
{
gotoxy(10, 12);
cout << "insufficient stock, available only " << stock;
cin.get();
goto LABEL1;
}
strstock = std::to_string(stock);
strquantity = std::to_string(quantity);
strrate = std::to_string(rate);
strtotal = std::to_string(total);
date *dt = new date;
time_t now = time(0);
tm *ltm = std::localtime(&now);
dt->yy = ltm->tm_year;
dt->mm = ltm->tm_mon;
dt->dd = ltm->tm_mday;
string strdate = std::to_string(1900 + dt->yy) + "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
sql = "select max(id) from tableBilling;";
qstate = mysql_query(mysql, sql.c_str());
int maxid;
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
maxid = 0;
}
else
{
maxid = atoi(row[0]);
}
++maxid;
strmaxid = std::to_string(maxid);
}
mysql_free_result(res);
}
strmemno = std::to_string(memno);
sql = "insert into tableBilling values ('"+strmaxid+"','"+strpid+"','"+strquantity+"','"+
strtotal+"', '"+strbillno+"','"+strmemno+"','"+strdate+"');";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(11, 11);
cout << "Billing data inserted ";
sql= "update tableProductRecords as p set p.stock = "+strqty+" where p.productid = "+strpid+";";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(10, 12);
cout << "stock data updated successfully";
gotoxy(31, 7);
cout << strqty;
cin.get();
}
else
{
gotoxy(10, 12);
cout << "error in billing data inserted " << mysql_error(mysql);
cin.get();
}
}
else
{
gotoxy(10, 11);
cout << "Error in insert : " << mysql_error(mysql);
cin.get();
return;
}
sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
strbillno+"' and productname in (select productname from tableProductRecords where productid = b.productid);";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(1, 13);
res = mysql_store_result(mysql);
sqlprocess *sqlp = new sqlprocess;
lines = sqlp->process_result_set(mysql, res);
}
else
{
gotoxy(1, 13);
cout << "error in select billing " << mysql_error(mysql);
}
gotoxy(10, 12);
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int i = 10; i < w.ws_col -1; i++)
{
cout << " ";
}
gotoxy(31, 17 + lines);
cout << "Grand Total : " << gtotal;
gotoxy(11,12);
cout << "want to add more records : ";
cin >> yesno;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toupper(yesno) == 'Y')
{
goto LABEL1;
}
if(less != 1)
{
float discount = 0.05 * gtotal;
gotoxy(31, 18 + lines);
cout << "Discount Rs. : " << discount;
gotoxy(31, 19 + lines);
cout << "Net Total : " << less * gtotal;
}
}
mysql_free_result(res);
mysql_close(mysql);
// delete con;
delete draw;
delete mysql;
delete res;
}i don't know why this error is occuring. Please sugest me.


here is code where segmentation fault occures.( //segmentation error is commented out here)
Code:void classbilling::billing()
{
clrscr();
drawrect();
mysql = Conn::connection();
char yesno;
int lines;
int billno;
int amount;
int instock;
int numrowsaffected;
int stock;
std::string strquantity;
long memno;
long cardno;
long memcardno;
std::string strcardno;
std::string stramxid;
std::string strbillno;
std::string strmemno;
std::string strstock;
std::string strrate;
std::string strtotal;
std::string strmaxid;
float less = 1.0;
MYSQL_RES *res;
MYSQL_ROW row;
gotoxy(10, 6);
cout << "Are you a member : ";
cin >> yesno;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toupper(yesno) == 'Y')
{
gotoxy(10, 7);
cout << "Enter Membership Number : ";
std::string strmemno;
getline(cin, strmemno);
int m = Conn::intvalidity(strmemno);
memno = m;
if(m == 0)
{
draw->gotoxy(10,10);
cout << "Enter Valid Membership number";
cin.get();
return;
}
sql = "select name from tableMemberRecords where membershipno = '"+strmemno+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
gotoxy(10, 10);
cout << "Invalid Membership number";
cin.get();
}
}
}
gotoxy(10, 8);
cout << "Enter Card Number : ";
std::string strcardno;
getline(cin, strcardno);
m = Conn::intvalidity(strcardno);
if(m == 0)
{
draw->gotoxy(10,10);
cout << "Enter Valid card number";
cin.get();
return;
}
//display of member
gotoxy(1, 12);
cout << "before memsearch ";
cin.get();
mem1 = mem2->memberSearch(memno );
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
cardno = m;
if(mem1->getmemcardno() != cardno)
{
gotoxy(10, 9);
cout << "Incorrect card number";
cin.get();
return;
}
gotoxy(1, 14);
cout << "after if mem1";
cin.get();
//strmemno = std::to_string(memno);
cin.get();
cout << "after to_string strmemeno : " << strmemno;
cin.get();
// mysql_free_result(res);
sql = "select CardNo, AmountDeposited from tableMemberRecords where MembershipNo = '"+strmemno+"';";
cout << "after sql : " << sql;
cin.get();
qstate = mysql_query(mysql, sql.c_str()); //segmentation error
gotoxy(1, 15);
cout << "after sql";
cin.get();
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res))!= nullptr)
{
memcardno = atol(row[0]);
amount = atoi(row[1]);
// gotoxy(1, 14);
}
}
else
{
gotoxy(1, 18);
cout << "Error in else of select cardno : " << mysql_error(mysql);
cin.get();
}
if(cardno != memcardno)
{
gotoxy(10, 14);
cout << "incorrect card number or membership number";
cin.get();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
else if(mem1->getmemamount() > 100)
{
less = 0.95f;
amount -= 5;
mem1->setmemamount(amount);
}
else
{
gotoxy(10, 13);
cout<< "Amount less than 100 ";
gotoxy(10, 14);
cout << "Continue with normal billing ";
cin >> yesno;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toupper(yesno) == 'N')
{
return;
}
}
gotoxy(1, 14);
cout << "before to_string";
cin.get();
string stramt = std::to_string(amount);
strcardno = std::to_string(cardno);
strmemno = std::to_string(memno);
gotoxy(1, 15);
cout << "before update sql";
cin.get();
sql = "update tableMemberRecords set amountdeposited = '"+stramt+"' where membershipno = '"+strmemno+"';";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(2, 16);
cout << "Member Record is updated and updated record is : ";
gotoxy(1, 17);
mem2->memberSearch(memno);//, 0, 15);
std::cin.get();
}
}
else
{
memno = 0;
}
sql = "select max(Billno) from tableBilling";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
billno = 0;
}
else
{
billno = atoi(row[0]);
}
strbillno = std::to_string(++billno);
}
}
product *prd = new product;
LABEL1:
clrscr();
drawrect();
sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
strbillno+"' and p.productname in (select productname from tableProductRecords where productid = b.productid);";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(1, 13);
res = mysql_store_result(mysql);
sqlprocess *sqlp = new sqlprocess;
numrowsaffected = sqlp->process_result_set(mysql, res);
}
productid = prd->displayProduct("PRODUCT DISPLAY");
if(productid == 0)
{
draw->gotoxy(10, 10);
cout << "Incorrect Product id";
cin.get();
// return;
goto LABEL1;
}
if(productid > 0)
{
clrscr();
drawrect();
getcmenu->printMenu(salesmenu, "Billing");
gotoxy(45, 3);
cout << "Bill No. : " << billno;
gotoxy(31, 5);
cout << productid;
string strpid = std::to_string(productid);
sql = "select productname, stock, rate from tableProductRecords where productid = '"+strpid+"';";
qstate = mysql_query(mysql,sql.c_str());
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
gotoxy(31, 6);
productname = row[0];
cout << productname;
gotoxy(31, 7);
stock = atoi(row[1]);
cout << stock;
gotoxy(31, 8);
rate = atoi(row[2]);
cout << rate;
}
mysql_free_result(res);
}
sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
strbillno+"' and productname in (select productname from tableProductRecords where productid = b.productid);";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(1,13);
res = mysql_store_result(mysql);
sqlprocess *sqlp = new sqlprocess;
lines = sqlp->process_result_set(mysql, res);
}
do
{
gotoxy(31, 9);
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int x = 31; x <= w.ws_col-1; x++)
{
cout << " ";
}
gotoxy(31, 9);
getline(cin, strquantity);
quantity = Conn::intvalidity(strquantity);
if(quantity == 0)
{
gotoxy(10, 10);
cout << "Invalid Quantity";
cin.get();
gotoxy(31, 9);
}
}while(quantity == 0);
gotoxy(15, 10);
cout << "Total : ";
int qty = stock - quantity;
if(qty >= 0)
{
strqty = std::to_string(qty);
total = quantity * rate;
gtotal += total;
gotoxy(31, 10);
cout << total;
}
else
{
gotoxy(10, 12);
cout << "insufficient stock, available only " << stock;
cin.get();
goto LABEL1;
}
strstock = std::to_string(stock);
strquantity = std::to_string(quantity);
strrate = std::to_string(rate);
strtotal = std::to_string(total);
date *dt = new date;
time_t now = time(0);
tm *ltm = std::localtime(&now);
dt->yy = ltm->tm_year;
dt->mm = ltm->tm_mon;
dt->dd = ltm->tm_mday;
string strdate = std::to_string(1900 + dt->yy) + "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
sql = "select max(id) from tableBilling;";
qstate = mysql_query(mysql, sql.c_str());
int maxid;
if(!qstate)
{
res = mysql_store_result(mysql);
if((row = mysql_fetch_row(res)) != nullptr)
{
if(row[0] == nullptr)
{
maxid = 0;
}
else
{
maxid = atoi(row[0]);
}
++maxid;
strmaxid = std::to_string(maxid);
}
mysql_free_result(res);
}
strmemno = std::to_string(memno);
sql = "insert into tableBilling values ('"+strmaxid+"','"+strpid+"','"+strquantity+"','"+
strtotal+"', '"+strbillno+"','"+strmemno+"','"+strdate+"');";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(11, 11);
cout << "Billing data inserted ";
sql= "update tableProductRecords as p set p.stock = "+strqty+" where p.productid = "+strpid+";";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(10, 12);
cout << "stock data updated successfully";
gotoxy(31, 7);
cout << strqty;
cin.get();
}
else
{
gotoxy(10, 12);
cout << "error in billing data inserted " << mysql_error(mysql);
cin.get();
}
}
else
{
gotoxy(10, 11);
cout << "Error in insert : " << mysql_error(mysql);
cin.get();
return;
}
sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
strbillno+"' and productname in (select productname from tableProductRecords where productid = b.productid);";
qstate = mysql_query(mysql, sql.c_str());
if(!qstate)
{
gotoxy(1, 13);
res = mysql_store_result(mysql);
sqlprocess *sqlp = new sqlprocess;
lines = sqlp->process_result_set(mysql, res);
}
else
{
gotoxy(1, 13);
cout << "error in select billing " << mysql_error(mysql);
}
gotoxy(10, 12);
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int i = 10; i < w.ws_col -1; i++)
{
cout << " ";
}
gotoxy(31, 17 + lines);
cout << "Grand Total : " << gtotal;
gotoxy(11,12);
cout << "want to add more records : ";
cin >> yesno;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(toupper(yesno) == 'Y')
{
goto LABEL1;
}
if(less != 1)
{
float discount = 0.05 * gtotal;
gotoxy(31, 18 + lines);
cout << "Discount Rs. : " << discount;
gotoxy(31, 19 + lines);
cout << "Net Total : " << less * gtotal;
}
}
mysql_free_result(res);
mysql_close(mysql);
// delete con;
delete draw;
delete mysql;
delete res;
}i don't know why this error is occuring. Please sugest me.