Modules/MySQL/MysqlQuery: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:


===Required Arguments===
===Required Arguments===
*'''db''' : A ''mysql'' object returned by [[mysqlOpen]]
*'''db''' : A ''mysql'' object created by [[mysqlCreate]]
*'''callback_function''' : The function that is called if the operation is done (please see below)
*'''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


Line 30: Line 30:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onMySQLResult ( table )
function onMySQLResult ( table )
outputServerLog ( "GOT RESULT" )
outputServerLog ( "Printing some test results" )
outputServerLog ( table[1][1] )
outputServerLog ( table[1][1] )
outputServerLog ( table[1][2] )
outputServerLog ( table[1][2] )
end
end


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


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

Revision as of 21:54, 29 January 2007


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 db, string callback_function, string query )

Required Arguments

  • db : 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