-2

I want to have the fields names separated with ',' . This query

show COLUMNS FROM ma_table;

Returns raw unseparated values

$sql = "show COLUMNS from ma_table "; 
$req = mysql_query($sql) or die('Erreur<br>'.$sql.'<br>'.mysql_error()); 
while ($rslt = mysql_fetch_assoc($req)) { 
    $fields=$rslt['Field']; 
} 
mysql_close();
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    You need to add your code in also. We cannot help if we cannot see – Option Jan 05 '17 at 12:31
  • Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) And how to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) – RiggsFolly Jan 05 '17 at 12:35
  • this is my code `$sql = "show COLUMNS from ma_table "; $req = mysql_query($sql) or die('Erreur SQL!
    '.$sql.'
    '.mysql_error()); while ($rslt = mysql_fetch_assoc($req)) { $fields=$rslt['Field']; } mysql_close();`
    – Hakim Amzaourou Jan 05 '17 at 12:38
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 05 '17 at 12:55
  • @sougatabose I dont think that is a particularly good dup for this question. Now OP posted code its just a simple _dont know what I am doing in a while loop issue_ – RiggsFolly Jan 05 '17 at 12:58

1 Answers1

0

All you need to do is concatenate a comma onto $fields as you process round the while loop.

You also need to use the .= string concatenation operator in the loop so you are adding each new Field to the string and not overwriting it each time.

$fields = '';
while ($rslt = mysql_fetch_assoc($req)) { 
    $fields .= $rslt['Field'] . ','; 
} 

echo rtrim($fields, ',');

I am afraid I must also add Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149