Raised This Month: $32 Target: $400
 8% 

MYSQL - How To Select a row and print it out


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 05-21-2019 , 12:13   MYSQL - How To Select a row and print it out
Reply With Quote #1

Hey, as the title say, how to select from a database and print out in chat, example: ("SELECT * FROM test WHERE name='%s'", userName);

and then in chat: You have .. Points.
iskenderkebab33 is offline
CliptonHeist
Senior Member
Join Date: Feb 2016
Old 05-21-2019 , 12:30   Re: MYSQL - How To Select a row and print it out
Reply With Quote #2

PHP Code:
public void SomeFunction(int Client)
{
    if(!
IsClientInGame(Client)) return;

    
char sQuery[1024], userName[64];
    
GetClientName(ClientuserNamesizeof(userName));
    
g_Database.Escape(userNameuserNamesizeof(userName));

    
Format(sQuerysizeof(sQuery), "SELECT * FROM test WHERE name='%s';"userName);
    
g_Database.Query(SQL_SelectTestsQueryGetClientUserId(Client));
}

public 
void SQL_SelectTest(Database dbDBResultSet results, const char[] sErrorany data)
{
    if(
results == null)
    {
        
LogError("MySQL Query Failed: %s"sError);
        return;
    }

    if(!
results.FetchRow()) return;

    
int Client GetClientOfUserId(data);
    if(!
IsClientInGame(Client)) return;

    
int pointsCol;
    
results.FieldNameToNum("points"pointsCol);
    
int points results.FetchInt(idCol);
    
PrintToChat(Client"You have %i Points."points);

Obviously you need a valid database connection before doing any of this which you can see how to do here: https://wiki.alliedmods.net/SQL_(Sou...)#Connecting_2
CliptonHeist is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 05-21-2019 , 12:31   Re: MYSQL - How To Select a row and print it out
Reply With Quote #3

Quote:
Originally Posted by CliptonHeist View Post
PHP Code:
public void SomeFunction(int Client)
{
    if(!
IsClientInGame(Client)) return;

    
char sQuery[1024], userName[64];
    
GetClientName(ClientuserNamesizeof(userName));
    
g_Database.Escape(userNameuserNamesizeof(userName));

    
Format(sQuerysizeof(sQuery), "SELECT * FROM test WHERE name='%s';"userName);
    
g_Database.Query(SQL_SelectTestsQueryGetClientUserId(Client));
}

public 
void SQL_SelectTest(Database dbDBResultSet results, const char[] sErrorany data)
{
    if(
results == null)
    {
        
LogError("MySQL Query Failed: %s"sError);
        return;
    }

    if(!
results.FetchRow()) return;

    
int Client GetClientOfUserId(data);
    if(!
IsClientInGame(Client)) return;

    
int pointsCol;
    
results.FieldNameToNum("points"pointsCol);
    
int points results.FetchInt(idCol);
    
PrintToChat(Client"You have %i Points."points);

Obviously you need a valid database connection before doing any of this which you can see how to do here: https://wiki.alliedmods.net/SQL_(Sou...)#Connecting_2
thanks, will do it.
iskenderkebab33 is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 05-23-2019 , 10:02   Re: MYSQL - How To Select a row and print it out
Reply With Quote #4

and how to get text row like from "sirname" ("SELECT * FROM test WHERE name='%s', userName") and print it out in chat: Your sirname is X.
iskenderkebab33 is offline
CliptonHeist
Senior Member
Join Date: Feb 2016
Old 05-23-2019 , 10:06   Re: MYSQL - How To Select a row and print it out
Reply With Quote #5

PHP Code:
public void SomeFunction(int Client

    if(!
IsClientInGame(Client)) return; 

    
char sQuery[1024], userName[64]; 
    
GetClientName(ClientuserNamesizeof(userName)); 
    
g_Database.Escape(userNameuserNamesizeof(userName)); 

    
Format(sQuerysizeof(sQuery), "SELECT * FROM test WHERE name='%s';"userName); 
    
g_Database.Query(SQL_SelectTestsQueryGetClientUserId(Client)); 


public 
void SQL_SelectTest(Database dbDBResultSet results, const char[] sErrorany data

    if(
results == null
    { 
        
LogError("MySQL Query Failed: %s"sError); 
        return; 
    } 

    if(!
results.FetchRow()) return; 

    
int Client GetClientOfUserId(data); 
    if(!
IsClientInGame(Client)) return; 

    
int nameCol
    
results.FieldNameToNum("sirname"nameCol); 
    
char name[64]; results.FetchString(nameColnamesizeof(name)); 
    
PrintToChat(Client"Your sirname is %s."name); 

CliptonHeist is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 05-25-2019 , 13:47   Re: MYSQL - How To Select a row and print it out
Reply With Quote #6

how to print in chat to client if sirname is empty? like PrintToChat(client, "Sirname is empty. Please give a sirname"); thanks
iskenderkebab33 is offline
CliptonHeist
Senior Member
Join Date: Feb 2016
Old 05-25-2019 , 19:31   Re: MYSQL - How To Select a row and print it out
Reply With Quote #7

PHP Code:
if(StrEqual(sName"");
    
PrintToChat(Client"Surname is empty, please give surname.); 
CliptonHeist is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 05-26-2019 , 04:48   Re: MYSQL - How To Select a row and print it out
Reply With Quote #8

Better to use:
Code:
if ( sName[ 0 ] == '\0' ) // or even sName[ 0 ] == 0
    PrintToChat( Client, "Surname is empty, please give surname." );
It's better for performance.
__________________
MAGNAT2645 is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 05-26-2019 , 05:51   Re: MYSQL - How To Select a row and print it out
Reply With Quote #9

thanks you 2, gona test it
iskenderkebab33 is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 05-26-2019 , 08:47   Re: MYSQL - How To Select a row and print it out
Reply With Quote #10

and how to do a if() example:

if(sirname = "Tom")
{
PrintToChat(Client, "You sirname is Tom");
}
else
{
PrintToChat(Client, "You sirname is NOT Tom");
}

Last edited by iskenderkebab33; 05-26-2019 at 08:47.
iskenderkebab33 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 01:26.


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