<?php
$link=mysql_connect("localhost", "root","parola") or die("Could not connect: " . mysql_error());
mysql_select_db('energiesolara',$link) or die ('Can\'t use energiesolaradb: ' . mysql_error());

$x=mysql_query("SELECT umiditate FROM parametrii");
while($info=mysql_fetch_array($x))
{
$comma_separated=implode(",",$info);

}
print "$comma_separated";
?>


I want that $comma_separated to store all the elements.But when I print that there is only one element which is doubled,because of $info I think.Pls help me.tnx

<?php
$link=mysql_connect("localhost", "root","parola") or die("Could not connect: " . mysql_error());
mysql_select_db('energiesolara',$link) or die ('Can\'t use energiesolaradb: ' . mysql_error());

$x=mysql_query("SELECT umiditate FROM parametrii");
while($info=mysql_fetch_array($x))
{
$comma_separated=implode(",",$info);

}
print "$comma_separated";
?>


I want that $comma_separated to store all the elements.But when I print that there is only one element which is doubled,because of $info I think.Pls help me.tnx

Correct me if I'm wrong, but it seems $comma_separated only has the last elements of your table. Let say there are 10 elements in the table, the first time the loop runs $comma_separated will contain the first element, the second time only the second and so on.
Change the line to

$comma_separated .= implode(",",$info);

The . (point) before = will append the new data.

U're right.I fix the problem in other way.I add [] after $comma_separated.tnx

anyway I don't understand why every element is printed twice:(

Actually, what colweb said will not end up with what you want, since your implode will put a comma between elements from each row, but will results from different rows wil not have a comma between them.

The double values you get is from the fact that you use mysql_fetch_array - which returns in fact 2 sets of data, one with numeric keys for fields and one with associative keys;
your $info variable will be an array like this:
$info[0] = 'value of umiditate';
$info = 'value of umiditate';
When you implode $info (which you shouldn't), you actually join the 2 values from $info, which are in fact the value of the same field - hence, the double value separated by comma.
I recomend you use mysql_fetch_assoc in your scripts since it is faster than mysql_fetch_array and doesn't bring back two sets of data.

So, you can do this two ways:
like colweb said, but with a small adjustment.

$comma_separated = '';
while($info=mysql_fetch_assoc($x))
{
$comma_separated .= $info['umiditate'] . ','; // 
}
$comma_separated = substr($comma_separated, -1, 1); // remove the comma at the end
print "$comma_separated";

or

while($info=mysql_fetch_assoc($x))
{
$comma_separated[] = $info['umiditate']; // 
}
$comma_separated = implode(",", $comma_separated);
print "$comma_separated";

tnx.it works! (ms mult:P)

Cu placere ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.