[MYSQL] Either queries or sscanf crash the server
#1

Okay so i had this problem for a long time and can't get it fixed. But i know it is probably in the mysql_get_field or the sscanf.
pawn Код:
//  format(str, sizeof(str), "SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='%s'", name);
//  mysql_query(str);
//  mysql_get_field("HouseID", str); //Gets the first field of the row returned (HouseID)
//  mysql_free_result();
    if(!sscanf(str, "d", integer))
This way it does not make it crash... Uncommenting it will make it crash. But it also says with mysql_get_field("HouseID", str); it is empty while its really not:

So why is it killing my server with this:
pawn Код:
format(str, sizeof(str), "SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='%s'", name);
    mysql_query(str);
    mysql_get_field("HouseID", str); //Gets the first field of the row returned (HouseID)
    mysql_free_result();
    if(!sscanf(str, "d", integer))
Reply
#2

You only free the results with "mysql_free_result();" after you have collected all your data.

Side note, I suggest upgrading to threaded queries when you get the time.

Quote:

if(!sscanf(str, "d", integer))

What does this do? You haven't posted the code under it.
Reply
#3

Код:
    format(str, sizeof(str), "SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='%s'", name);
    mysql_query(str);
    mysql_store_result();
    mysql_get_field("HouseID", str); //Gets the first field of the row returned (HouseID)
    mysql_free_result();
    if(!sscanf(str, "d", integer))
Reply
#4

The thing that you have totally forgot to store the result so basically is empty
pawn Код:
mysql_store_result();
And also an advise for SQL. Use either mysql_real_escape_string or mysql_format
because:

Quote:
Important Note: Always use this function (if you don't use mysql_format()) before inserting user inputs in a query. You can be victim of a SQL injection if you do not do so.

Reply
#5

Okay thanks it does not crash anymore now. But it still outputs this:
Код:
[19:45:02] CMySQLHandler::FetchField(HouseID) - You cannot call this function now. (Reason: Fields/Rows are empty.)

[19:45:02] >> mysql_query( Connection handle: 1 )

[19:45:02] CMySQLHandler::Query() - An error has occured. (Error ID: 1065, Query was empty)

[19:45:02] >> mysql_free_result( Connection handle: 1 )

[19:45:02] CMySQLHandler::FreeResult() - The result is already empty.

[19:45:05] >> mysql_query( Connection handle: 1 )
Even though they is clearly like shown in the original post photo a vehicle in there. Any idea why it does this? This i s what it shows when there is indeed no car in the table with that name. It is exactly the same.
Код:
[13:24:15] CMySQLHandler::Query(SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='Thimo2') - Successfully executed.

[13:24:15] >> mysql_store_result( Connection handle: 1 )

[13:24:15] CMySQLHandler::StoreResult() - Result was stored.

[13:24:15] >> mysql_fetch_field_row( Connection handle: 1 )

[13:24:15] CMySQLHandler::FetchField(HouseID) - You cannot call this function now. (Reason: Fields/Rows are empty.)

[13:24:15] >> mysql_free_result( Connection handle: 1 )

[13:24:15] CMySQLHandler::FreeResult() - Result was successfully free'd.

[13:24:18] >> mysql_real_escape_string( Connection handle: 1 )
When it does work it should show Car loaded in the console
Also thanks for the mysql_real_escape_string !
Reply
#6

I didn't understand which one is the "current" log. Can you show us the log of the problem you face and the part of code you execute?
Reply
#7

Used the code magnetec wrote and fixed it. It should work now!

Код:
format(str, sizeof(str), "SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='%s'", name);
new result[16] = mysql_query(str);
mysql_store_result();
mysql_get_field("HouseID", result); //Gets the first field of the row returned (HouseID)
mysql_free_result();
new hID = strval(result);
That will give the variable 'hID' the house-id.
Reply
#8

Okay sure. The problem is that it does not load the row that is actually there and reports an error back saying that the row is empty. While i have proof that they are not empty.
Heres the log:
Код:
[13:10:08] CMySQLHandler::Query(SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='Thimo') - Successfully executed.

[13:10:08] >> mysql_store_result( Connection handle: 1 )

[13:10:08] CMySQLHandler::StoreResult() - Result was stored.

[13:10:08] >> mysql_fetch_field_row( Connection handle: 1 )

[13:10:08] CMySQLHandler::FetchField(HouseID) - You cannot call this function now. (Reason: Fields/Rows are empty.)

[13:10:08] >> mysql_free_result( Connection handle: 1 )

[13:10:08] CMySQLHandler::FreeResult() - Result was successfully free'd.
The code i am TRYING to excecute is this:

pawn Код:
format(str, sizeof(str), "SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='%s'", name);
    mysql_real_escape_string(str, string1);
    mysql_query(string1);
    mysql_store_result();
    mysql_get_field("HouseID", string1); //Gets the first field of the row returned (HouseID)
    mysql_free_result();
    if(!sscanf(str, "d", integer))
    {
        if(integer != 0) //Player has a house...
        {
            format(string, sizeof(string), "SELECT * FROM `Vehicles` WHERE `HouseID`= '%d'", integer);
            mysql_query(string);
            if(mysql_num_rows() != 0)
            {
                while(mysql_num_rows()) //Loop xD
                {
                    //Do what you want with the vehicle :P
                    //Use db_get_field or db_get_field_assoc
                    //Remember to store the vehicle IDs in a variable so you can delete them in OnPlayerDisconnect
                    mysql_get_field("Model", cModel);
                    mysql_get_field("X", cx);
                    mysql_get_field("Y", cy);
                    mysql_get_field("Z", cz);
                    mysql_get_field("Rot", crot);
                    mysql_get_field("Color1", Col1);
                    mysql_get_field("Color2", Col2);
                    new hId = strval(str);
                    House_AddVehicle(hId, strval(cModel), floatstr(cx), floatstr(cy), floatstr(cz), floatstr(crot), strval(Col1), strval(Col2));
                    print("Car loaded");
                }
                print("Does work");
            }

        }
    }
Where this part goes wrong:

pawn Код:
format(str, sizeof(str), "SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='%s'", name);
    mysql_real_escape_string(str, string1);
    mysql_query(string1);
    mysql_store_result();
    mysql_get_field("HouseID", string1); //Gets the first field of the row returned (HouseID)
    mysql_free_result();
Reply
#9

Got it!

Change to:

pawn Код:
if(!sscanf(string1, "d", integer))
You store the HouseID to string1, when str stores the query!

It's not necessary to use mysql_real_escape_string for the whole query, just the input which is string.
Reply
#10

Well thats still not working. When it loads it should echo to the console to say Car loaded. But it shows nothing.
This is the log:
Код:
[23:34:54] CMySQLHandler::EscapeString(SELECT `HouseID` FROM `Houses` WHERE `Houseowner`='Thimo'); - Escaped 59 characters to SELECT `HouseID` FROM `Houses` WHERE `Houseowner`=\'Thimo\'.

[23:34:54] >> mysql_query( Connection handle: 1 )

[23:34:54] CMySQLHandler::Query(SELECT `HouseID` FROM `Houses` WHERE `Houseowner`=\'Thimo\') - An error has occured. (Error ID: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'Thimo\'' at line 1)

[23:34:54] >> mysql_store_result( Connection handle: 1 )

[23:34:54] CMySQLHandler::StoreResult() - No data to store.

[23:34:54] >> mysql_fetch_field_row( Connection handle: 1 )

[23:34:54] CMySQLHandler::FetchField(HouseID) - You cannot call this function now. (Reason: Fields/Rows are empty.)

[23:34:54] >> mysql_free_result( Connection handle: 1 )

[23:34:54] CMySQLHandler::FreeResult() - The result is already empty.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)