Spyder Tech Spyder Tech - 1 month ago 7
MySQL Question

Getting Array from MySQL Column

I'm storing an array of comma separated values in my database in a column called

line_services
. The data in the column looks like
1,1,1,1,0,0,0
and I have a prepared statement where I bind the results of this column to the variable
$line_services
. In this prepared statement I have the following code after my fetch.

foreach(explode(',', $line_services) as $lineservices) {
if ($lineservices == '1') {
strtolower(str_replace('','_',$service_name.'_total')) = ((($service_tax / 100) * $service_cost) + 80.00) * $line_artwork_hours; //1 hour
}
if ($lineservices == '2') {
strtolower(str_replace('','_',$service_name.'_total')) = ((($service_tax / 100) * $material_cost) + $material_cost) * $line_product_sqft; //2 sq ft
}
if ($lineservices == '3') {
strtolower(str_replace('','_',$service_name.'_total')) = (($service_tax / 100) * ($line_L + $line_W)) + ($line_L + $line_W); //3 linear
}
if ($lineservices == '4') {
strtolower(str_replace('','_',$service_name.'_total')) = (($service_tax / 100) * (($line_L + $line_W) * 2) * .75) + (($line_L + $line_W) * 2) * .75; //4 linear
}
}


I'm getting the following error:


Can't use function return value in write context


When I do a
var_dump
on
$line_services
it is coming up
NULL
even though there is data in the column. The line_services column in the database is a varchar if that makes any difference.

Answer

I assume you are trying for a variable variables? Rarely if ever a good idea, but you can use the curly syntax:

${strtolower(str_replace('','_',$service_name.'_total'))} = 'whatever'; 

Consider using an array instead:

$total[$service_name] = 'whatever';