Skip to content
Advertisement

How can I determine mySQL prepared statement result column names in PHP?

That is, with a prepared statement like:

JavaScript

I believe I can use $mysqli->field_count to get the number of columns being returned (haven’t tried).

But is there a way to link each column name to the values returned in bind_results? I could always try to parse the column names out from the command itself, but that’s not a road I want to go down.

Context:

I want to be able to define a PHP interface that maps class properties to column names returned from the database. So given

JavaScript

and

JavaScript

I can use bind_results and then do something like (pseudoish code)

JavaScript

to pull out just the two columns I need (col1 and col2) and assign them to the correct properties of class A.

Advertisement

Answer

I believe mysqli_stmt::result_metadata and mysqli_result::fetch_fields could help you.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement