Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using a query to get all the column names from my database, then I am trying to display the results comma seperated, in quotation marks and in (). And also make variables out of them.(Take a look at the example at the bottom and you will probably understand) The Columns I have in my DB are:

ID  
Naam     
Email
Soort
Status

And I am using this to get them:

$strSQL = "select column_name from information_schema.columns where table_name='Klant'";  
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); 
$objResult = mysql_fetch_array($objQuery);

So I was wondering If there is any way to get the result displayed like this:

$vaaw       = array("(ID)", "(Naam)", "(Email)", "(Soort)", "(Status)");

And also like this:

$vervang    = array("$ID", "$Naam", "$Email","$Soort", "$Status");

I want to use this instead of typing the column names in my code because if I ever change the column name or add/delete a column then I will have to edit the codes again.

I have been looking for a way to get this for hours now but I just can't find anything to display it like this. I think it could be possible using the implode function but I couldn't find anything like this. I hope my question is clear but if not just ask for clarification!

Also, I know I shouldn't be using Mysql_ anymore but I am working on that and will change to PDO soon!

share|improve this question
1  
Try hard not to change column names! To help you if you add a column in the future, consider writing all your sql with the column names explicitly mentioned vis: INSERT INTO table (col1,col2,col3) VALUES ('1','2','3'); this means that wherever you enter your columns your old code will always work. For name changes, if you must, why not use edit pad pro. It's superb for programming and allows a global search/replace across multiple files. I love it. before anyone else nags - mysqli_ PDO? –  Robert Seddon-Smith Jun 4 '13 at 7:54
 
@RobertSeddon-Smith I know I should't be changing the column names but I thought if I someday had to change the code would keep working. Thnx for the suggestion for edit pad pro, ill take a look at it. And about the mysql_ PDO thingy, im working on that^^ –  Daanvn Jun 4 '13 at 8:05
add comment

1 Answer

You want something like this:

$data = array(
  "ID" => 1,
  "Naam" => 2,
  "Email" => "[email protected]",
  "Soort" => 4,
  "Status" => 5
);

echo '$vaav = array("(' . implode( ')", "(' , $data) . ')") ;' ;

echo '$vervang = array("' . implode( '", "' , $data) . '") ;' ;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.