Modules/MySQL/MysqlQuery: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 17: Line 17:
===Callback Arguments===
===Callback Arguments===
Your callback function has to accept the following arguments:
Your callback function has to accept the following arguments:
*'''mysql_result''' rows : Returns the rows in a [[mysql_result]] array type
On success:
*'''int''' rows : Returns the amount of rows
 
*'''table:''' The 2-dimensional table where the results are stored in: table [row_index] [column_index].
 
On failure:
 
*'''boolean:''' False, when no rows are found or an error occured.


===Optional Arguments===
===Optional Arguments===
Line 25: Line 30:


==Example==
==Example==
  function onMySQLResult ( mysql_result, rows )
<syntaxhighlight lang="lua">
    if ( rows > 0 )
function onMySQLResult ( table )
      -- we got a valid result, so let's process it
outputServerLog ( "GOT RESULT" )
 
outputServerLog ( table[1][1] )
      -- we're only expecting 1 row to return (there shouldn't be any nick duplicates)
outputServerLog ( table[1][2] )
      player_nick = [[mysqlGetField]] ( mysql_result, 0, 0 );
end
      player_money = [[mysqlGetField]] ( mysql_result, 0, 1 );
 
      player_carid = [[mysqlGetField]] ( mysql_result, 0, 2 );
function onMySQLOpen ( mysql, result )
 
if ( result ) then
      -- clean it up!
outputServerLog ( "CONNECTED" )
      [[mysqlCleanupResult]] ( mysql_result );
mysqlQuery ( mysql, "onMySQLResult", "SELECT * FROM test" )
    else
else
      -- no rows were returned
outputServerLog ( "DIDNT WORK" )
    end
end
  end
end
  <...>
 
  [[mysqlQuery]] ( my_mysql, "onMySQLResult", "SELECT `nickname`,`money`,`carid` FROM `rpg_players` WHERE `nickname` = 'ORLY'" );
function mysqltest ()
mysqlOpen ( "onMySQLOpen", "localhost", "bastage", "bastage_pw", "test", 3306 )
end
</syntaxhighlight>

Revision as of 01:18, 1 November 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 both mysqlGetField and mysqlCleanupResult to deal with the mysql_result object returned by this function.

Syntax

bool mysqlQuery ( mysql object, string callback_function, string query )

Required Arguments

  • mysql : The mysql object returned by mysqlOpen
  • 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: On success:

  • table: The 2-dimensional table where the results are stored in: table [row_index] [column_index].

On failure:

  • boolean: False, when no rows are found or an error occured.

Optional Arguments

None

Example

function onMySQLResult ( table )
	outputServerLog ( "GOT RESULT" )
	outputServerLog ( table[1][1] )
	outputServerLog ( table[1][2] )
end

function onMySQLOpen ( mysql, result )
	if ( result ) then
		outputServerLog ( "CONNECTED" )
		mysqlQuery ( mysql, "onMySQLResult", "SELECT * FROM test" )
	else
		outputServerLog ( "DIDNT WORK" )
	end
end

function mysqltest ()
	mysqlOpen ( "onMySQLOpen", "localhost", "bastage", "bastage_pw", "test", 3306 )
end