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

mysql connect and fetch rows


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BFBombi
Junior Member
Join Date: Jul 2011
Old 03-16-2012 , 16:01   mysql connect and fetch rows
Reply With Quote #1

Hello, i have a problem, but can't resolve it.

Code:
public OnPluginStart()
{
	...
	SQL_TConnect(GotDatabase, "vote_vs");
}

public GotDatabase(Handle:owner, Handle:hndl, const String:error[], any:data)
{
	if (hndl == INVALID_HANDLE)
    {
        SetFailState("Error connecting to database. %s", error);
    } else {
        Database = hndl;
    }
}  

public OnMapStart()
{
	g_VoteNum = 0;
	s_VoteNumFirst = 0;
	s_VoteNumSecond = 0;
	
	s_VoteNameFirst = "";
	s_VoteNameSecond = "";
	
	new String:query[255];
    Format(query, sizeof(query), "SELECT * FROM vote ORDER BY id DESC LIMIT 1");
    new Handle:query_r = SQL_Query(Database, query);
	
	if(SQL_FetchRow(query_r) == false) {
		PrintToServer("[Votes] Can't fetch row.");
	} else {
	    PrintToServer("[Votes] Fetched row");
        g_VoteNum = SQL_FetchInt(query_r, 0);
		s_VoteNumFirst = SQL_FetchInt(query_r, 1);
		s_VoteNumSecond = SQL_FetchInt(query_r, 2);
		SQL_FetchString(query_r, 3, s_VoteNameFirst, 256);
		SQL_FetchString(query_r, 4, s_VoteNameSecond, 256);
		
    }
	
    CloseHandle(query_r);
}
It give me an error
Code:
Native "SQL_Query" reported: Invalid  database Handle 0 (error: 4)
Database setting is all okey!
BFBombi is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 03-16-2012 , 18:03   Re: mysql connect and fetch rows
Reply With Quote #2

First: Use threaded queries, and second: you're not allowing enough time to connect to the database, most likely.
necavi is offline
BFBombi
Junior Member
Join Date: Jul 2011
Old 03-17-2012 , 08:34   Re: mysql connect and fetch rows
Reply With Quote #3

Quote:
Originally Posted by necavi View Post
second: you're not allowing enough time to connect to the database, most likely.
How i can fix it ?
BFBombi is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 03-17-2012 , 15:46   Re: mysql connect and fetch rows
Reply With Quote #4

Put your query in the callback for TConnecting to the database, right under your Database = hndl line.
necavi is offline
BFBombi
Junior Member
Join Date: Jul 2011
Old 03-18-2012 , 08:05   Re: mysql connect and fetch rows
Reply With Quote #5

Thaks!

Last edited by BFBombi; 03-18-2012 at 08:38.
BFBombi is offline
Reply



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 19:51.


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