Problem with MYSQL

This is a discussion on Problem with MYSQL within the C++ Programming forums, part of the General Programming Boards category; I am using the Mysql C api and the DevPack made for this. But when I am using this code, ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Problem with MYSQL

    I am using the Mysql C api and the DevPack made for this. But when I am using this code, I always get an Illegal Operation message when running the program:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <mysql/mysql.h>
    
    using namespace std;
    
    int main()
    {
        char *host = "sql1.badblock.com";
        char *user = "theuser";
        char *pass = "thepassword";
        char *db = "thedb";
        MYSQL_RES *result;
        MYSQL *res;
    
        MYSQL *sock;
        sock = mysql_init(0);
        if (sock) cout << "sock handle ok!" << endl;
        else {
             cout << "sock handle failed!" << endl;
             return EXIT_FAILURE;
        }
    
        if (res=mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
             cout << "connection ok!" << endl;
        else {
             cout << "connection failed!" << endl;
             return EXIT_FAILURE;
        }
        
        mysql_query(res,"SELECT address FROM music WHERE pass=euro8");
        cout<<"Query done!";
        result=mysql_store_result(res);
        cout<<"Store result done!";
    MYSQL_FIELD *field;
    cout<<"mysql field done!";
    
    while(field = mysql_fetch_field(result))
    {
    cout<<"in while!";
        cout<<field;
    }
    cout<<"while done!";
    cin.get();
        mysql_close(sock);
    
        return EXIT_SUCCESS;
    }
    So I added some couts to see how far the program got, before illegal operation message appears. The last cout displayed is mysql field done. Any ideas?
    Last edited by maxorator; 08-25-2005 at 02:51 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    As a first step, add a << flush to each of your debug printing lines to make sure they are printed immediatly and not buffered and forgotten when your app crashes:

    Example:
    Code:
    cout << "in while" << flush;
    What gets printed when you cout the field pointer ? I don't know the API, but maybe

    Code:
    cout << field->name << flush;
    is more appropriate.

    If both doesn't help, your best bet is getting a debugger. Does Dec-Cpp have one ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    same results...

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Debug reported:
    An access violation (segmantation fault) raised in your program

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Well I just tested your program I get a connection failed
    Woop?

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    because I dont use my correct username password and database

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>result=mysql_store_result(res);

    Your not checking result for NULL.

    >>SELECT address FROM music WHERE pass=euro8

    Might that be "SELECT address FROM music WHERE pass='euro8'"

    ??

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It doesnt help, still I get the illegal operation message
    Last edited by maxorator; 08-25-2005 at 03:24 AM.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by maxorator
    It doesnt help, still I get the illegal operation message
    Have you tried running you're query through the MySQL Query Browser?

    Does it return the recordset you expect?

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    You also need to check the result of mysql_query to see if it is non zero.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Installing MySQL
    By cerin in forum Tech Board
    Replies: 2
    Last Post: 02-18-2006, 12:30 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Problem connecting to MySQL Database?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2006, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21