Imploding a MySQL array
I'm having trouble imploding a MySQL database array, below is a sample of the code I am using:
mysql_select_db($database_wlast, $wlast);
$query_category = "SELECT product_name FROM raw_materials WHERE category = '$_POST[category]'";
$category = mysql_query($query_category, $wlast) or die(mysql_error());
$row_category = mysql_fetch_array($category);
$result= implode(',',$row开发者_Python百科_category);
echo $result;
The result is the following:
Amber glass bottle 100ml, Amber glass bottle 100ml
i.e: it is spitting out the first value in the array twice and nothing else.
Please help!
while($row_category = mysql_fetch_array($category)){
$result[] = implode(',',$row_category);
}
echo implode("\n",$result);
That's how mysql_fetch_array() returns the result, twice: a numeric key and a string key. You possibly want mysql_fetch_assoc()
.
You want to implode all your product name from your database? if yes, than your code above is wrong.
mysql_fetch_array($category) only return 1 row of your database. If you want to implode of your all product name than code should like this:
$result="";
while($row_category=mysql_fetch_array($category))
$result=$result.",".$row[0];
or you some thing like this. You hold all your product name on array and implode it. Like this:
while($row_category=mysql_fetch_array($category))
$result[]=$row[0];
$newResult=implode(",",$result);
use mysql_fetch_array($category, MYSQL_NUM)
or mysql_fetch_row($category)
or mysql_fetch_assoc($category)
The problem is that by default mysql_fetch_array
returns enumerated and associative arrays combined.
精彩评论