Hello! I do a sample of varchar data from a table like this:

$query = "SELECT * FROM words"; $res = mysql_query($query); while($row = mysql_fetch_array($res)) { echo "Номер: ".$row['he']."<br>\n"; $list = $row['he'] } 

Tell me how to make it so that all the data from the table is selected and each word is entered into the array separated by a space?

To make it like this:

 $list = "JAVA BEANS PHP SCRIPTS SOURCE CODE JAVASCRIPT GAMES SSI IS SERVER SIDE INCLUDES BILL GATES COOKIES HTTP AUTHENT; 
  • Why do you select all fields from the table via * if you need to manipulate only one column as a result? - Alexey Shimansky
  • right, in one column. But how to do so to select all the data from this column and drive them into an array? - Decarti

1 answer 1

You can use at least two ways.

1) Do this in the query itself:

 SELECT GROUP_CONCAT(`he` SEPARATOR " ") FROM `words` 

As a result, immediately get a string of words, separated by a space http://webi.ru/webi_articles/8_14_f.html

2) Select words by the he field of the words table As a result, we get an array of words. Let's call it $resultQuery . And further to the result apply implode http://php.net/manual/ru/function.implode.php

 $words = implode(' ', $resultQuery); 

Just as a reminder about the mysql_ extension

Warning This extension is deprecated since PHP 5.5.0 and will be removed in the future. Use MySQLi or PDO_MySQL instead. See also the MySQL instruction: API selection and the corresponding FAQ for more details. Alternatives to this function: mysqli_connect () PDO :: __ construct ()

http://php.net/manual/ru/function.mysql-connect.php

  • thanks for the help! Could you tell me more - checked the query in php MySQL - everything works fine! but why do I get only the first word in the array? I do this: $query = "SELECT wo, GROUP_CONCAT(wo SEPARATOR ' ') FROM words"; $res = mysql_query($query); while($row = mysql_fetch_array($res)) { $list = $row['wo']; } $query = "SELECT wo, GROUP_CONCAT(wo SEPARATOR ' ') FROM words"; $res = mysql_query($query); while($row = mysql_fetch_array($res)) { $list = $row['wo']; } $query = "SELECT wo, GROUP_CONCAT(wo SEPARATOR ' ') FROM words"; $res = mysql_query($query); while($row = mysql_fetch_array($res)) { $list = $row['wo']; } - Decarti
  • @ Vasiliy Because the sample group_concat in the field name receives GROUP_CONCAT(wo SEPARATOR ' ') and you still select the wo field for some reason .... you need to concatenate the group name SELECT GROUP_CONCAT(wo SEPARATOR ' ') as words_list FROM words and then you will have $row['words_list'] ..... and since the request will only give one immediately concatenated row, it will not need to be processed in the loop. It is enough to write $row = mysql_fetch_array($res); $list = $row['words_list']; $row = mysql_fetch_array($res); $list = $row['words_list']; and if the version is php > = 5.4, then in general $list = mysql_fetch_array($res)['words_list'] - Alexey Shimansky
  • Thank you very much @ Alexey Shimansky for help! Your methods and advice helped me a lot! I will take note of the extension))) - Decarti