myqsl_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 :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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, &times, totallen);
    }
    else
    {
        cout << " error : " << "  "<< mysql_error(mysql) << "  " << endl;
    }
    cin.get();
}

output :-

1
2
library initialized     mysql_init initialised  mysql_real_connect returned 0 : mysql = 0
Segmentation fault (core dumped)
Last edited on
You should know this by now...
https://www.cplusplus.com/articles/jEywvCM9/
Registered users can post here. Sign in or register to post.