Thread: mysql_use_result() returns NULL problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    mysql_use_result() returns NULL problem

    Hello everybody,
    I recently moved from PHP to C++ and started to learn MySQL DB connections. When I try to execute a query, mysql_use_result leads to access violation error and I can't figure out why. The code is:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    #include <mysql.h>
    #pragma link "libmySQL.lib"
    
    MYSQL * db_connect;
    MYSQL_RES *res, *res1;
    MYSQL_ROW row;
    
    using namespace std;
    
    char *server = "localhost";
    char *user = "root";
    char *password = "";
    char *database = "f";
    
    char Query[256];
    
    char input_user_login[32];
    char* user_login;
    char input_user_password[32];
    char* user_password;
    char* user_id;
    char* user_db_password;
    
    int CHAR_STR;
    int CHAR_HP;
    
    void choose_char() {
    
    sprintf(Query, "SELECT id, str, hp FROM characters WHERE user_id=%s",user_id);
    
       mysql_query(db_connect, Query);
    
       res = mysql_use_result(db_connect);
    
       while ((row = mysql_fetch_row(res)) != NULL) {
    	  cout << row[0] << "      " << row[1] << "     " << row[2] << "\n";
    	}
    
    cout << "Choose a character id:\n";
    char character[32];
    cin >> character;
    
    sprintf(Query, "SELECT str, hp FROM characters WHERE id=%s",character);
    
    if (mysql_query(db_connect, Query)) {
    	  cout << "Error getting the info \n";
     }
    
    	res = mysql_use_result(db_connect);
    	row = mysql_fetch_row(res);
    	CHAR_STR = atoi(row[0]);
    	CHAR_HP = atoi(row[1]);
    
       mysql_free_result(res);
       mysql_close(db_connect);
    
    }
    void user_login_screen() {
    
    
    	start:
    
    	cin >> input_user_login;
    	cin >> input_user_password;
    
    	user_login = (char*)input_user_login;
    	user_password = (char*)input_user_password;
    
    	sprintf(Query, "SELECT id, password FROM users WHERE login=%s",user_login);
    
    
    	mysql_query(db_connect, Query);
    
       res = mysql_use_result(db_connect);
    
       if ((row = mysql_fetch_row(res)) == NULL) {
    	   cout << "This user is not in the DB\n\n";
    	   goto start;
       }
    
       user_id = row[0];
       user_db_password = row[1];
    
       if (strcmp(user_password, user_db_password) != 0) {
    	   cout << "Wrong login or password.\n\n";
    	   goto start;
       }
    
    }
    
    int main() {
    
    	db_connect = mysql_init(NULL);
    
    	if (!mysql_real_connect(db_connect, server,
    		 user, password, database, 0, 0, 0)) {
    	cout << "Could not connect to the DB";
    	   }
    	else {
    		   cout << "Connection succesful!\n";
    	}
    
    
            user_login_screen();
    	cout << "Log in successful\n";
    	Sleep(1000);
    
    	choose_char();
    
    return(0);
    }
    The problem is login goes alright with no problems, but when choose_char() is called, I get access violation because res = mysql_use_result(db_connect); is NULL.

    Help please

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Sorry, I posted it in the wrong section, chould be C++ :/

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ Programming forum.

    Just a few pointers to help you help us to help you:
    • It is great that you posted in code tags, but you also should indent your code properly.
    • Do not use the goto keyword; it looks like you only need a loop.
    • Avoid global variables: they make it more difficult to reason about your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thanks for the tips.

    I've been fighting with this for 2 days and the solution was easy as pie (like always). Using mysql_store_result would fix all access violation errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread