SA-MP Forums Archive
SQL won't load houses. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SQL won't load houses. (/showthread.php?tid=343302)



SQL won't load houses. - iGetty - 17.05.2012

I have this function:

pawn Код:
LoadHouses()
{
    mysql_query("SELECT NULL FROM houses");
    mysql_store_result();
    new rows = mysql_num_rows();
    mysql_free_result();

    CheckMySQL();

    for(new i; i < rows; i++) {
        new string[128];
        format(string, sizeof(string), "SELECT * FROM houses WHERE ID = '%d'", i);
        mysql_query(string);
        mysql_store_result();

        new row[128];
        new field[15][24];
        mysql_fetch_row_format(row, "|");
        explode(row, field, "|");
        mysql_free_result();
        HD[i][ID] = strval(field[0]);
        format(HD[i][owner], 32, "%s", field[1]);
        format(HD[i][hname], 24, "%s", field[2]);
        HD[i][hprice] = strval(field[3]);
        HD[i][exteriorX] = floatstr(field[4]);
        HD[i][exteriorY] = floatstr(field[5]);
        HD[i][exteriorZ] = floatstr(field[6]);
        HD[i][interiorX] = floatstr(field[7]);
        HD[i][interiorY] = floatstr(field[8]);
        HD[i][interiorZ] = floatstr(field[9]);
        HD[i][interiorInt] = strval(field[10]);
        HD[i][locked] = strval(field[11]);
        HD[i][gunslot1] = strval(field[12]);
        HD[i][locklevel] = strval(field[13]);
        HD[i][safeamount] = strval(field[14]);
        if(strlen(HD[i][owner]) > 0) CreatePickup(1273, 1, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ], -1);
        else CreatePickup(1273, 1, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ], -1);
        Create3DTextLabel(HD[i][hname], 0x000000AA, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ], 5.0, 0);
        printf("House %d spawned @ %f, %f, %f", i, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ]);
    }
}
Yet it doesn't spawn. All it says on the "printf" is:

House 0 spawned @ 0.000000, 0.000000, 0.000000

Thanks.


Re: SQL won't load houses. - Mandrakke - 17.05.2012

what CheckMySQL(); does?


Re: SQL won't load houses. - iGetty - 17.05.2012

pawn Код:
stock CheckMySQL()
{
    if(mysql_ping() == -1)
        mysql_connect(HOST, USER, DATABASE, SQLPassword);
}
That's all.


Re: SQL won't load houses. - Mandrakke - 17.05.2012

So, are you connecting in to database after making an query? or you use this function before the first query as well? if so, put it somewhere into your script and paste here the output;

pawn Код:
public OnQueryError(errorid, error[], resultid, extraid, callback[], query[], connectionHandle)
{
    printf("%d %s %d %d %s %s %d",errorid, error, resultid, extraid, callback, query, connectionHandle);
    return 1;
}



Re: SQL won't load houses. - iGetty - 17.05.2012

I've got that in and it doesn't print anything.


Re: SQL won't load houses. - Mandrakke - 17.05.2012

ok, try it;

pawn Код:
LoadHouses() {
    new Query[70], temp[50], i;
    format(Query, sizeof(Query), "SELECT * FROM houses");
    mysql_query(Query);
    mysql_store_result();

    CheckMySQL();
   
    while(mysql_fetch_row(Query)){
        mysql_fetch_field_row(HD[i][owner], "owner");
        mysql_fetch_field_row(HD[i][hname], "hname");
        mysql_fetch_field_row(temp, "ID");          HD[i][ID] = strval(temp);
        mysql_fetch_field_row(temp, "hprice");      HD[i][hprice] = strval(temp);
        mysql_fetch_field_row(temp, "exteriorX");   HD[i][exteriorX] = floatstr(temp);
        mysql_fetch_field_row(temp, "exteriorY");   HD[i][exteriorY] = floatstr(temp);
        mysql_fetch_field_row(temp, "exteriorZ");   HD[i][exteriorZ] = floatstr(temp);
        mysql_fetch_field_row(temp, "interiorX");   HD[i][interiorX] = floatstr(temp);
        mysql_fetch_field_row(temp, "interiorY");   HD[i][interiorY] = floatstr(temp);
        mysql_fetch_field_row(temp, "interiorZ");   HD[i][interiorZ] = floatstr(temp);
        mysql_fetch_field_row(temp, "interiorInt"); HD[i][interiorInt] = strval(temp);
        mysql_fetch_field_row(temp, "locked");      HD[i][locked] = strval(temp);
        mysql_fetch_field_row(temp, "gunslot1");    HD[i][gunslot1] = strval(temp);
        mysql_fetch_field_row(temp, "locklevel");   HD[i][locklevel] = strval(temp);
        mysql_fetch_field_row(temp, "safemount");   HD[i][safemount] = strval(temp);
       
        if(strlen(HD[i][owner]) > 0) CreatePickup(1273, 1, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ], -1);
        else CreatePickup(1273, 1, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ], -1);
        Create3DTextLabel(HD[i][hname], 0x000000AA, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ], 5.0, 0);
        printf("House %d spawned @ %f, %f, %f", i, HD[i][exteriorX], HD[i][exteriorY], HD[i][exteriorZ]);
       
        ++i;
    }
   
    mysql_free_result();
}
I assumed that you named your variables HD[i]["name"] with the same column name in to your table, if don't, just rename it.

if it won't work, debug the variable "rows" and see if it is returning something bigger than 0.


Re: SQL won't load houses. - iGetty - 17.05.2012

Awesome! That worked!

Thanks! I'd rep you but I can't :P


Re: SQL won't load houses. - Mandrakke - 17.05.2012

no problem, I will get some sleep now