Supposing a query such as:
SELECT * FROM tableA;
How can I prepend a_ to each columns’ name? For example if there is a column “username” it would be accessed in the results as “a_username”.
EDIT: The SELECT username AS a_username format will not help as I need to continue using the * field selection. There is a JOIN and a potential conflict with a returned column from another table in the JOIN. I will be iterating over the returned columns (foreach) and only want to output the columns that came from a particular table (whose schema may change) to HTML input fields where a site admin could edit the fields’ content directly. The SQL query in question looks like SELECT firstTable.*, anotherTable.someField, anotherTable.someOtherField and their exists the possibility that someField or someOtherField exists in firstTable.
Thanks.
You can use the INFORMATION_SCHEMA.COLUMNS table to formulate the query and then use dynamic SQL to execute it.
First let’s make a sample database called
dotancohenand a table calledmytableHere is the metadata table called INFORMATION_SCHEMA.COLUMNS:
What you need from this table are the following columns:
What you are asking for is to have the column_name and the column_name prepended with
a_Here is the query and how to execute it:
Let’s execute it
Give it a Try !!!
You mentioned in your question : The SELECT username AS a_username format will not help as I need to continue using the * field selection.
All you have to do to implement my suggestion is run the query using tableA as follows:
When you retrieve that query result, just use it as the query to submit to mysql_query.