Raised This Month: $12 Target: $400
 3% 

Help with SQL O_o


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 09-03-2010 , 14:24   Help with SQL O_o
Reply With Quote #1

PHP Code:
public bool:OnClientConnect(idString:szRejectMsg[], iMaxLen)
{
    
decl String:szAuth[36]
    
GetClientAuthString(idszAuthsizeof(szAuth))
    
    
decl String:szQuery[96]
    
FormatEx(szQuerysizeof(szQuery), "SELECT * FROM spunky_db WHERE Auth=\"%s\""szAuth)
    
SQL_TQuery(g_hDatabaseLoadDataszQueryidDBPrio_High)
    
    return 
true
}

public 
LoadData(Handle:hOwnerHandle:hQuery, const String:szError[], any:_Data)
{
    if (
hQuery == INVALID_HANDLE)
    {
        
LogError("Database failure: %s"szError)
        
        return
    }
    
    new 
id _:_Data
    
    
if (!SQL_FetchRow(hQuery))
    {
        
LogError("Database failure: Failed to fetch initial row")
        
        return
    }
    
    if (!
SQL_GetRowCount(hQuery))
    {
        
decl String:szAuth[36]
        
GetClientAuthString(idszAuthsizeof(szAuth))
        
        
decl String:szQuery[128]
        
FormatEx(szQuerysizeof(szQuery), "INSERT INTO spunky_db VALUES (\"%s\", 1, 0, 100, 1, 0, 100)"szAuth)
        
SQL_TQuery(g_hDatabaseCreateDataszQuery_DBPrio_High)
        
        
g_iHumanLevel[id] = 1
        g_iHumanExperience
[id] = 0
        g_iHumanMaxExperience
[id] = 100
        g_iZombieLevel
[id] = 1
        g_iZombieExperience
[id] = 0
        g_iZombieMaxExperience
[id] = 100
        
        
return
    }
    
    
g_iHumanLevel[id] = SQL_FetchInt(hQuery1)
    
g_iHumanExperience[id] = SQL_FetchInt(hQuery2)
    
g_iHumanMaxExperience[id] = SQL_FetchInt(hQuery3)
    
g_iZombieLevel[id] = SQL_FetchInt(hQuery4)
    
g_iZombieExperience[id] = SQL_FetchInt(hQuery5)
    
g_iZombieMaxExperience[id] = SQL_FetchInt(hQuery6)

Error:
Quote:
Database failure: Failed to fetch initial row
There are results to be fetched in the database, so I don't understand why this is happening, and the documentation is horrible at best.
Spunky is offline
Send a message via AIM to Spunky
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 09-03-2010 , 17:08   Re: Help with SQL O_o
Reply With Quote #2

None of the records in the database match the query - this could be because the client is not guaranteed to have a verified SteamID by the time OnClientConnect is called, and szAuth is actually empty. Try putting the query in OnClientAuthorized instead.
__________________
Scone is offline
Antithasys
Moderator
Join Date: Apr 2008
Old 09-03-2010 , 17:12   Re: Help with SQL O_o
Reply With Quote #3

Quote:
Originally Posted by Scone View Post
None of the records in the database match the query - this could be because the client is not guaranteed to have a verified SteamID by the time OnClientConnect is called, and szAuth is actually empty. Try putting the query in OnClientAuthorized instead.
OnClientPostAdminCheck() is best.
__________________
[my plugins]

When you think about asking a question... consider what have you tried?
Antithasys is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 09-03-2010 , 17:20   Re: Help with SQL O_o
Reply With Quote #4

and I do believe SQL doesn't use ", but '
And try to name your columns, will help a great deal in the end.
Easier to read and debug. For example: Now you don't know which order it might be in without knowing the intimacies of the tables.
For example: No way of knowing if you meant to start with the second column rather than first. (SQL_Fetch works with index, so starts at 0)
I assume it'll look something like this. (Probably different column names, and I added Auth so your indexes later in that function work out)

FormatEx(szQuery, sizeof(szQuery), "SELECT Auth, Level, Exp, MaxExp, ZLevel, ZExp, ZMaxExp FROM spunky_db WHERE Auth='%s'", szAuth)
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 09-03-2010 , 23:48   Re: Help with SQL O_o
Reply With Quote #5

SQL accepts double quotations just fine. They're actually preferred in most cases. Selecting every column by name is tedious and not worth any shred of effort. My query does the same thing and is easier to manage.

Anyway, I fixed it.

PHP Code:
public LoadData(Handle:hOwnerHandle:hQuery, const String:szError[], any:_Data)
{
    if (
hQuery == INVALID_HANDLE)
    {
        
LogError("Database failure: %s"szError)
 
        return
    }
 
    new 
id _:_Data
 
    
if (!SQL_GetRowCount(hQuery))
    {
        
decl String:szAuth[36]
        
GetClientAuthString(idszAuthsizeof(szAuth))
 
        
decl String:szQuery[128]
        
FormatEx(szQuerysizeof(szQuery), "INSERT INTO spunky_db VALUES (\"%s\", 1, 0, 100, 1, 0, 100)"szAuth)
        
SQL_TQuery(g_hDatabaseCreateDataszQuery_DBPrio_High)
 
        
g_iHumanLevel[id] = 1
        g_iHumanExperience
[id] = 0
        g_iHumanMaxExperience
[id] = 100
        g_iZombieLevel
[id] = 1
        g_iZombieExperience
[id] = 0
        g_iZombieMaxExperience
[id] = 100
 
        
return
    }
 
    else if (!
SQL_FetchRow(hQuery))
    {
        
LogError("Database failure: Failed to fetch initial row")
 
        return
    }
 
    
g_iHumanLevel[id] = SQL_FetchInt(hQuery1)
    
g_iHumanExperience[id] = SQL_FetchInt(hQuery2)
    
g_iHumanMaxExperience[id] = SQL_FetchInt(hQuery3)
    
g_iZombieLevel[id] = SQL_FetchInt(hQuery4)
    
g_iZombieExperience[id] = SQL_FetchInt(hQuery5)
    
g_iZombieMaxExperience[id] = SQL_FetchInt(hQuery6)


Last edited by Spunky; 09-04-2010 at 05:50.
Spunky is offline
Send a message via AIM to Spunky
Monkeys
Veteran Member
Join Date: Jan 2010
Old 09-04-2010 , 07:45   Re: Help with SQL O_o
Reply With Quote #6

Quote:
Originally Posted by Spunky View Post
SQL accepts double quotations just fine. They're actually preferred in most cases. Selecting every column by name is tedious and not worth any shred of effort. My query does the same thing and is easier to manage.

Anyway, I fixed it.
I've always had problems when using ", so I decided not to saves me the trouble of finding out if it's actualy '' I wrote or ".

And not worth any effort? You really don't like other people reading your code, do you? :p
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:32.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode