Can I somehow get an array of column names in the correct order when working with mysqli in php? As a result of JOIN the names of some of them may coincide.
2 answers
If you have a mysqli_result object, then access to the query metadata is done using the methods mysqli_result::fetch_fieldXXX
The object mysqli_result can be obtained mysqli_query() functions mysqli_query() , mysqli_store_result() , mysqli_use_result() . Or the mysqli_stmt_result_metadata() function, if you use prepared statements.
The list of fields in the field object is as follows:
- name column name
- orgname Original column name if it has an alias
- table The name of the table that owns the column (if not calculated)
- orgtable Source table name if alias
- max_length The maximum width of the result set field.
- length The length of the field in bytes, as defined when defining the table. Please note that this value (in bytes) may differ from the value in characters specified in the table field definition, since in different encodings one character can be written in several bytes. For example, the VARCHAR (10) field in the UTF-8 encoding will return 30 = 10 characters * 3 bytes per character, and 10% for the LATIN1 encoding, since one character occupies one byte in this encoding.
- charsetnr Numeric encoding id.
- flags An integer representing the bit flags for the field.
- type Data type of the field
- decimals Number of decimal places (for integer fields)
|
Use alias.
select t1.name as name1, t2.name as name2 from table_one as t1, table_two as t2 List all fields of interest from the selection, select the same under the alias
|