0

I am writing some MYSQL embedded into C++ and I am getting some behavior that I haven't been able to pinpoint the cause of.

I have a main function and two functions that can search by author or publisher. If I search by author, I get the proper results the first time. If I search by publisher, I don't ever get any results the first time. However, no matter what I do, the second time I call one of the functions I get a segmentation fault at the line "mysql_fetch_row" in the while loops. I'm assuming I am leaving some kind of connection open that is conflicting with the second call but I have not been able to figure it out.

Sorry for the wall of code but I'm not sure where in the program the issue is coming from. I tried to describe each part and noted where the program crashes in each of the two functions.

Main function, connects to the database and calls the other two functions

#include <mysql.h>
#include <iostream>
#include <string>

using namespace std;

void searchByAuthor(MYSQL *connect);
void searchByPublisher(MYSQL *connect);

int main()
{
    int choice = 0;

    MYSQL *connect, mysql;
    connect = mysql_init(&mysql);

    //connect to the database
    connect = mysql_real_connect(connect, SERVER, USER, PASSWORD, DATABASENAME, 0,
        NULL, 0);

 //Get user input here to select what to search by
 //Loops until user quits the program

    return 0;

}

Search By Author - Gets the user input and makes the SQL call. Returns results correctly the first time.

void searchByAuthor(MYSQL *connect)
{
    string name;
    string sql = "select Title, Price, Type, AuthorFirst, AuthorLast from Book, Author, Wrote where Author.AuthorNum=Wrote.AuthorNum and Wrote.BookCode = Book.BookCode and AuthorLast=\'" + name + "\'";

    MYSQL_RES *res_set;
    MYSQL_ROW row;

    cout << "Enter the last name of the author" << endl;
    cin >> name;

    mysql_query(connect, (sql).c_str());

    res_set = mysql_store_result(connect);

//Crashes here on second call
    while ((row = mysql_fetch_row(res_set)) != NULL)
    {
        cout << endl << row[0] << " "
            << row[1] << " " << row[2] << " "
            << row[3] << " " << row[4] << endl;

    }

    mysql_free_result(res_set);
    mysql_close(connect);
}

Search By Publisher - Gets user input and makes the SQL call - Does not ever return any results.

void searchByPublisher(MYSQL *connect)
{
    string publisher;
    string sql = "select Book.Title from Book, Publisher where Publisher.PublisherName =\'" + publisher + "\' and Publisher.PublisherCode = Book.PublisherCode";

    MYSQL_RES *res_set;
    MYSQL_ROW row = NULL;

    cout << "Enter the name of the publisher" << endl;
    cin >> publisher;

    mysql_query(connect, sql.c_str());

    res_set = mysql_store_result(connect);

//Crashes here on second call
    while ((row = mysql_fetch_row(res_set)) != NULL) 
    {

        cout << endl << row[0] << endl;
    }

    mysql_free_result(res_set);
    mysql_close(connect);
}
9
  • Wouldn't it skip right past the while loop then? Or is that not what you are referring to? Commented May 1, 2015 at 1:39
  • I have checked and it returns a few valid values. None of which are null. Commented May 1, 2015 at 1:43
  • @JoachimPileborg Even if that's happening, why would it cause a segfault when calling mysql_fetch_row? The segfault should happen when trying to print row[0] if it's null. Commented May 1, 2015 at 1:44
  • Yes, it is mysql_fetch_row that is causing the crash. I put a cout between the fetch and the output, and the cout doesn't get called. Commented May 1, 2015 at 1:46
  • You did run in a debugger to pinpoint the crash location, did you not? Then you would know exactly on what line in your code the crash happens, if it's in the mysql_fetch_row function call, or when you do std::cout << .... Commented May 1, 2015 at 1:47

2 Answers 2

2

You close the connection using mysql_close(connect) but not sure if you reopen the connection before querying again? Check here when you close it the handle is also closed.

Also check your mysql database if you have any null values in it.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Sometime's things like that can be staring at you right in the face and you still miss it. I am still not getting any result back when searching by publisher. However, I get results when I manually query the database with the same statement, and I get results when I use "select * from Book" in my function. I read somewhere that it might have to do with the single quotes in the statement, but I haven't been able to figure it out. Any ideas?
0

your mistake is in mysql_init(). the parameter of mysql_init must be NULL not &mysql.

It must be so:

connect = mysql_init(NULL);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.