Getting vehicle ID from a mysql query
#1

Alright im just scripting around, and I ran into an issue.. I can't figure out how to get a vehicle ID from the mysql query.. For example..


pawn Код:
CMD:vehicleinfo(playerid,params[])
{
    new string[128];
    new plate[11];
    new query[400];
    if(sscanf(params, "s[11]", plate)) return SendClientMessage(playerid, COLOR_RED, "> The correct usage of this command would be: /vehicleinfo vehicleplate");
   
    format(query, sizeof(query), "SELECT * FROM Vehicles WHERE Plate = '%s'", plate);
    mysql_query(query);
    mysql_store_result();
   
    return 1;
}
Normally I would throw a
pawn Код:
sscanf(Query, "p<|>e<idffffddds[11]ddds[24]ddd>", Vehicles[id]);
In there, but since Im not use a loop to load all vehicles, I honestly have no idea on how to get the vehicle id from the query I just did, Any help would be appreciated.
Reply
#2

PHP код:
CMD:vehicleinfo(playerid,params[])
{
    new 
string[128];
    new 
plate[11];
    new 
query[40+sizeof(plate)];
    if(
sscanf(params"s[11]"plate)) return SendClientMessage(playeridCOLOR_RED"> The correct usage of this command would be: /vehicleinfo vehicleplate");
    
    
format(querysizeof(query), "SELECT * FROM Vehicles WHERE Plate = \'%s\'"plate);
    
mysql_query(query);
    
mysql_store_result();
    
    if(
mysql_retrieve_row()) {
        new 
content[40];
        
mysql_fetch_field_row(content"modelid");
        
        
// the string "content" now contains the field "modelid"
        
        
new model strval(content);
        
        
// the var "model" now has the model from the database as an integer
    
}
    
    else {
        
SendClientMessage(playeridCOLOR_RED"That vehicle doesn't exist.");
    }
    
    return 
1;

It that didn't help, be more specific.
Reply
#3

Alright for the
pawn Код:
format(query, sizeof(query), "SELECT * FROM Vehicles WHERE Plate = \'%s\'", plate);
Whats the
pawn Код:
\'%s\'
I've only done '%s' before, whats the difference?

Also, just to ensure im not stupid, I could then go about by doing something like

pawn Код:
Vehicles[model][Ownername]
Or would that not work?
Reply
#4

Quote:
Originally Posted by Abreezy
Посмотреть сообщение
Alright for the
pawn Код:
format(query, sizeof(query), "SELECT * FROM Vehicles WHERE Plate = \'%s\'", plate);
Whats the
pawn Код:
\'%s\'
I've only done '%s' before, whats the difference?
The \ is an escape character, you are supposed to escape quotes (', ") in SQL queries to avoid SQL injection. It's important in this situation, because you're directly taking a player's input into a query.
Quote:
Originally Posted by Abreezy
Посмотреть сообщение
Also, just to ensure im not stupid, I could then go about by doing something like

pawn Код:
Vehicles[model][Ownername]
Or would that not work?
What EXACTLY is the purpose of this vehicleinfo command? Are you trying to load cars from a table and store their values into variables, or just fetch data from the table to show in a clientmessage?
Reply
#5

pawn Код:
if(mysql_retrieve_row())
    {
        new content[40];
        mysql_fetch_field_row(content, "SQLID");

        new vid = strval(content);
       
        format(string, sizeof(string), "> Vehicle Owner: %s ", Vehicles[vid][Owner]);
        SendClientMessage(playerid, COLOR_WHITE, string);
    }
This worked perfectly, thank you for your input.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)