Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sand_Hawk

mySQL Query returns weird values

This topic is 5933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I try to read a table out of my DB. But everytime it gives really weird values back. I do a check if the query is succesfull and if I can fetch the value. It works fine. What''s wrong? It is an incredible high integer value wich I can''t find out why. Here is the code:
  
int DeleteCategory(MYSQL *pConnection)
{
    MYSQL_RES   *pResult;
    MYSQL_ROW   sqlRow;
    char        Input[255];
    char        Query[255];
     
    system("cls");
    sprintf(Query, "Select * FROM Category");
    mysql_query(pConnection, Query);
    pResult = mysql_use_result(pConnection);

    while (sqlRow = mysql_fetch_row(pResult))
    {
        cout << sqlRow[0] << ". [" << sqlRow[1] << "]" << endl;
    }
    cout << endl;
    cout << "Wich category must be deleted?";
    gets(Input);

    sprintf(Query, "SELECT * FROM Category WHERE ID = %s", Input);
    if (mysql_query(pConnection, Query) != 0)
    {
        cout << "Error" << endl;
        getch();
        return (0);
    }
    pResult = mysql_use_result(pConnection);
    if ((sqlRow = mysql_fetch_row(pResult)) == NULL)
    {
        cout << "Error(2)" << endl;
        getch();
        return (0);
    }
    
    // This cout gives the error. In the sprintf the same val goes wrong

    cout << "Category ID: " << (unsigned int) sqlRow[2] << endl;
    mysql_free_result(pResult);

    sprintf(Query, "Delete FROM Products WHERE ParentID = %d", sqlRow[2]);
    cout << Query << endl;

    if (mysql_query(pConnection, Query))
    {
        cout << endl << "Error deleting products." << endl;
        cout << "Error number:  " << mysql_errno(pConnection) << endl;
        cout << "Error message: " << mysql_error(pConnection) << endl;
        getch();
        return (0);
    }

    sprintf(Query, "DELETE FROM Category WHERE ID = %s", Input);
    mysql_query(pConnection, Query);
    cout << "Category deleted!" << endl;
    getch();

    return (0);
}
  
Member of the Stupid Coders. http://www.stupidcoders.cjb.net -Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
Advertisement
Anybody?

Member of the Stupid Coders.
http://www.stupidcoders.cjb.net

-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
MYSQL_ROW is an array of strings, in fact, if you look at the headers, it''s typedef''ed to char **. That means that sqlRow[2] is a char *. Casting it to unsigned int will just print out the address of the pointer as an integer (which will be a really big number).

Replace the offending lines with either this:


cout << "Category ID: " << sqlRow[2] << endl;
sprintf( Query, "DELETE FROM Products WHERE ParentID=%s", sqlRow[2] );

mysql_free_result( pResult );


or this:


unsigned int id = atoi( sqlRow[2] );
mysql_free_result( pResult );

cout << "Category ID: " << id << endl;
sprintf( Query, "DELETE FROM Products WHERE ParentID=%d", id );


Note, don''t free the result until you''re done using the rows. You should put the mysql_free_result() line where I''ve put it, since you use the row data in the next line.


codeka.com - Just click it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!