Modules/MTA-MySQL/mysql fetch field: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: __NOTOC__ Returns a table with information about a field in a query result. You can call repeatedly this function to return all the result fields, and when it reaches the end returns nil. ...)
 
mNo edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
<pageclass class="#AA7592" subcaption="MTA-MySQL Module"></pageclass>
__NOTOC__
__NOTOC__
{{ModuleFunction|MTA-MySQL}}
Returns a table with information about a field in a query result. You can call repeatedly this function to return all the result fields, and when it reaches the end returns nil. You can also go to a specific field using [[Modules/MTA-MySQL/mysql_field_seek|mysql_field_seek()]].
Returns a table with information about a field in a query result. You can call repeatedly this function to return all the result fields, and when it reaches the end returns nil. You can also go to a specific field using [[Modules/MTA-MySQL/mysql_field_seek|mysql_field_seek()]].


Line 42: Line 44:
   end
   end
end
end
mysql_free_result(result) -- Free the result
</syntaxhighlight>
</syntaxhighlight>


==See also==
==See also==
{{Modules/MTA-MySQL/Result_functions}}
{{Modules/MTA-MySQL/Result_functions}}

Latest revision as of 18:02, 5 January 2011


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

Returns a table with information about a field in a query result. You can call repeatedly this function to return all the result fields, and when it reaches the end returns nil. You can also go to a specific field using mysql_field_seek().

Syntax

table mysql_fetch_field ( MySQLResult result )

Required arguments

  • result: A valid MySQL result

Returns

A table with information about a field with the following keys:

  • name: The name of this field.
  • org_name: If you used aliases in the query, the original name of the field.
  • table: The table of this field.
  • org_table: If you used aliases for the table, the original name of the table.
  • length: The maximum length allowed by this field in the table definition.
  • max_length: The maximum length of this field in all the result rows.
  • not_null: True if the field can't be NULL (See mysql_null()).
  • primary_key: True if the field is the table primary key.
  • unique_key: True if the field value is unique.
  • multiple_key: True if the field is part of a key.
  • numeric: True if the field is numeric.
  • blob: True if the field is a BLOB.
  • unsigned: True if the field is unsigned.
  • zerofill: True if the field is zero filled.
  • type: A string representing the type of this field.

Example

Example 1: This example shows how to print the rows of a result set showing the field name.

local result = mysql_query(handler, "SELECT * FROM account") -- Execute the query
for result,row in mysql_rows(result) do -- Iterate through all the result rows
  mysql_field_seek(result, 1) -- Reset the field cursor to the first field
  for k,v in ipairs(row) do
    local field = mysql_fetch_field(result) -- Retreive the field data
    if (v ~= mysql_null()) then
      outputDebugString("row[" .. field["name"] .. "] = " .. v)
    else
      outputDebugString("row[" .. field["name"] .. "] = NULL")
    end
  end
end
mysql_free_result(result) -- Free the result

See also