MySQL question
#1

Hello,

I want to do this:

Code:
mysql_query("SELECT * FROM `players` WHERE `Name` = '%s'", inputtext);
All right, now, How can I advertise when a "Name" selected doesn't exist in the table?

Thank you.
Reply
#2

Check the num rows.

Code:
mysql_query("SELECT * FROM `players` WHERE `Name` = '%s'", inputtext);
mysql_store_result();
if(!mysql_num_rows()) return SendClientMessage(playerid, -1, "User DOESN'T exist");
Reply
#3

which version of MySQL you have?. if is the last you can do something like this:

PHP Code:
stock AccountExist(name[])
{
    new
        
str[64];
        
    
format(strsizeof str"SELECT * FROM `players` WHERE `Name` = '%s'"name);
    new
        
Cache:result mysql_query(MySQL_Connectionstr),
        
val = ((cache_num_rows() > 0) ? (true) : (false));
    
cache_delete(result);
    return 
val;

or

PHP Code:
stock AccountExist(name[])
{
    new
        
str[64];
    
format(strsizeof str"SELECT * FROM `players` WHERE `Name` = '%s'"name);
    
mysql_query(str); 
    
mysql_store_result();
    
    new
        
TRUE mysql_num_rows() > true false;
    
mysql_free_result();
    
    return 
TRUE;

PHP Code:
if(!AccountExist(inputtext))
{
    
// the account not exist

Reply
#4

Quote:
Originally Posted by KyleSmith
View Post
Check the num rows.

Code:
mysql_query("SELECT * FROM `players` WHERE `Name` = '%s'", inputtext);
mysql_store_result();
if(!mysql_num_rows()) return SendClientMessage(playerid, -1, "User DOESN'T exist");
THANKS YOU!!
Reply
#5

Quote:
Originally Posted by Yerro
View Post
THANKS YOU!!
Oops, don't forget to add mysql_free_result(); at the end of the query, after num_rows.
Reply
#6

Also escape the string because it could contain SQL injection code. (assuming inputtext is from OnDialogResponse)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)