Modules/MySQL/MysqlQuery: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:
This function is provided by the '''ml_mysql''' module.
This function is provided by the '''ml_mysql''' module.


Use [[mysqlGetRow]] and [[mysqlCleanupRow]] to deal with the ''mysql_result'' object returned by this function.
Use [[mysqlGetField]] and [[mysqlCleanupResult]] to deal with the ''mysql_result'' object returned by this function.


==Syntax==
==Syntax==
Line 27: Line 27:
     if ( rows > 0 )
     if ( rows > 0 )
       -- we got a valid result, so let's process it
       -- we got a valid result, so let's process it
       -- we're only expecting 1 row to return (there shouldn't be any nick duplicates)
       -- we're only expecting 1 row to return (there shouldn't be any nick duplicates)
       player_nick = mysqlGetRow ( mysql_result, 0, 0 );
       player_nick = mysqlGetField ( mysql_result, 0, 0 );
       player_money = mysqlGetRow ( mysql_result, 0, 1 );
       player_money = mysqlGetFow ( mysql_result, 0, 1 );
       player_carid = mysqlGetRow ( mysql_result, 0, 2 );
       player_carid = mysqlGetFow ( mysql_result, 0, 2 );
 
      -- clean it up!
      mysqlCleanupResult ( mysql_result );
     else
     else
       -- no rows were returned
       -- no rows were returned

Revision as of 02:37, 22 April 2006

Description

This functions queries the MySQL server through the MySQL connection that has been opened by mysqlOpen. The result of the query is then passed to the script by calling callback_function.

This function is provided by the ml_mysql module.

Use mysqlGetField and mysqlCleanupResult to deal with the mysql_result object returned by this function.

Syntax

bool mysqlQuery ( [string callback_function, string query] )

Required Arguments

  • callback_function : The function that is called if the operation is done (please see below)
  • query : The MySQL query that is sent

Callback Arguments

Your callback function has to accept the following arguments:

  • mysql_result rows : Returns the rows in a mysql_result array type
  • int rows : Returns the amount of rows

Optional Arguments

None

Example

 function onMySQLResult ( mysql_result, rows )
   if ( rows > 0 )
     -- we got a valid result, so let's process it
     -- we're only expecting 1 row to return (there shouldn't be any nick duplicates)
     player_nick = mysqlGetField ( mysql_result, 0, 0 );
     player_money = mysqlGetFow ( mysql_result, 0, 1 );
     player_carid = mysqlGetFow ( mysql_result, 0, 2 );
     -- clean it up!
     mysqlCleanupResult ( mysql_result );
   else
     -- no rows were returned
   end
 end
 <...>
 mysqlQuery ( "onMySQLResult", "SELECT `nickname`,`money`,`carid` FROM `rpg_players` WHERE `nickname` = 'ORLY'" );