Modules/MySQL/MysqlQuery: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(21 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
==Description==
{{ModuleFunction|MySQL}}
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.
This functions queries the MySQL server through the MySQL connection that has been opened by [[Modules/MySQL/MysqlOpen|mysqlOpen]]. The result of the query is then passed to the script by calling callback_function. It's syntax and functionality is similar to that of [[executeSQLSelect]].
 
Use [[mysqlGetRow]] and [[mysqlCleanupRow]] to deal with the ''mysql_result'' object returned by this function.


==Syntax==
==Syntax==
bool mysqlQuery ( [string callback_function, string query] )
<syntaxhighlight lang="lua">bool mysqlQuery ( mysql mysqlobj, string callback_function, string query )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
*'''callback_function''' : The function that is called if the operation is done (please see below)
*'''mysqlobj''' : A ''mysql'' object created by [[Modules/MySQL/MysqlCreate|mysqlCreate]]
*'''callback_function''' : The function that is called if the operation is done (see below)
*'''query''' : The MySQL query that is sent
*'''query''' : The MySQL query that is sent


===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
 
*'''bool''' result : Returns false on error, true on success
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===
===Optional Arguments===
Line 24: Line 28:


==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 ( "Printing some test results" )
      -- we're only expecting 1 row to return (there shouldn't be any nick duplicates)
outputServerLog ( table[1][1] )
      player_nick = mysqlGetRow ( mysql_result, 0, 0 );
outputServerLog ( table[1][2] )
      player_money = mysqlGetRow ( mysql_result, 0, 1 );
end
      player_carid = mysqlGetRow ( mysql_result, 0, 2 );
 
    else
function onMySQLOpen ( result )
      -- no rows were returned
if ( result ) then
    end
outputServerLog ( "MySQL connection established." )
  end
mysqlQuery ( db, "onMySQLResult", "SELECT * FROM test" )
  <...>
else
  mysqlQuery ( "onMySQLResult", "SELECT `nickname`,`money`,`carid` FROM `rpg_players` WHERE `nickname` = 'ORLY'" );
outputServerLog ( "MySQL connection failed." )
end
end
 
function mysqltest ()
db = mysqlCreate ()
mysqlOpen ( db, "onMySQLOpen", "localhost", "bastage", "bastage_pw", "test", 3306 )
end
</syntaxhighlight>

Latest revision as of 20:48, 29 September 2009


Package-x-generic.png This function is provided by the external module MySQL. You must install this module to use this function.


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. It's syntax and functionality is similar to that of executeSQLSelect.

Syntax

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

Required Arguments

  • mysqlobj : A mysql object created by mysqlCreate
  • callback_function : The function that is called if the operation is done (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 ( "Printing some test results" )
	outputServerLog ( table[1][1] )
	outputServerLog ( table[1][2] )
end

function onMySQLOpen ( result )
	if ( result ) then
		outputServerLog ( "MySQL connection established." )
		mysqlQuery ( db, "onMySQLResult", "SELECT * FROM test" )
	else
		outputServerLog ( "MySQL connection failed." )
	end
end

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