mysql_real_connect returns 0
hi to all, I've mysql server in centos 6 VM and client in centos 7 VM. I've building a small project using c++ and mysql. when i want to connect to server in program it shows segmentation fault. And after searching for bug I got mysql_real_connect returns nullptr. I've created a class Conn and static function connection(); which i call as MYSQL *mysql = Conn::connection(); mine code is :-
MYSQL * Conn::connection()
{
//MY_INIT(argv[0]);
mysql_init(mysql);
if(mysql_library_init(0,NULL, NULL))
{
std::cout << "mysql lib init " << std::endl;
exit(1);
}
else
{
std::cout << "library initialized\t";
}
// mysql = mysql_init(NULL);
// mysql = mysql_init(nullptr);
mysql = mysql_init(mysql);
if(mysql == NULL)
{
std::cout<< "mysql connector is NULL";
//cin.get();
exit(2);
}
else
{
std::cout << "mysql_init initialised\t";
}
// conn = mysql_real_connect (&mysql,"serverora.db.net","rahul","rahul","cbs",3306,NULL,0);
mysql = mysql_real_connect (mysql,opt_host,opt_user_name,opt_password,opt_db_name,opt_port,opt_socket_name,opt_flags);
if(mysql == 0)
{
std::cout << "mysql_real_connect returned 0 : ";
return 0;
}
else
{
std::cout << "mysql initialized";
}
return mysql;
}
in header :-
#ifndef STAT_H_INCLUDED
#define STAT_H_INCLUDED
#include <my_global.h>
#include <my_sys.h>
#include "draw.h"
#include "getchoice.h"
#include "product.h"
#include<iostream>
#include<cstdio>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<mysql.h>
#include <vector>
class Conn;
static char *opt_host = "serverora.db.net";
static char *opt_user_name = "rahul";
static char *opt_password = "rahul";
static unsigned int opt_port = 3306;
static char *opt_socket_name = NULL;
static char *opt_db_name = "cbs";
static unsigned int opt_flags = 0;
class Conn // public Draw
{
Draw * draw;
int *totallen;
public:
std::vector<std::string> menu;
static MYSQL *mysql;
static int qstate;
static MYSQL_ROW row;
static MYSQL_RES *res;
static MYSQL_FIELD *field;
static MYSQL* connection();
static void printError(MYSQL *conn, char * message);
static bool dateValidity(std::string date);
static long intvalidity(std::string strnum);
Conn();
~Conn();
};
#endif // STAT_H_INCLUDED
code where error occured:-
void product::showAllProduct()
{
int times;
char choose;
clrscr();
drawrect();
int l = 5;
gotoxy(15, 5);
cout << "Welcome To Electronic Store";
gotoxy(15, 6);
cout << "Show All Items Menu\n";
string strQuery = "select *from tableProductRecords order by productname asc;";
gotoxy(1, 10);
mysql = Conn::connection();
cout << "mysql = " << mysql;
int qState = mysql_query(mysql, strQuery.c_str());// here error occures
if(!qState)
{
res = mysql_store_result(mysql);
int i = process_result_set(mysql, res, 10, ×, totallen);
}
else
{
cout << " error : " << " "<< mysql_error(mysql) << " " << endl;
}
cin.get();
}
output :-
library initialized mysql_init initialised mysql_real_connect returned 0 : mysql = 0