Ref : Foreach loop declare variable on select table array
OK ..... No more syntax error
Same question
- Is it possible to create variable inside foreach loop that each variable name depend on column name that I select?
I've coded out like
$recid = $_GET['recid'];
//sql select string declaration
$sql = "select [Rec_ID],[Bike_ID],[Station],['Line']
from [rfttest].[dbo].[RFT_Records_Log]
where [Rec_ID] = {$recid}";
$query = sqlsrv_query($conn,$sql); //query
//if query fail print out error
if($query === false)
{
die(print_r(sqlsrv_errors(),true));
sqlsrv_close($conn);
}
//continue with fetch array
$recdata = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC);
//foreach to declare variable
foreach($recdata as $x => $a)
{
$"$x" = $"$a";
}
Result should be
$Rec_ID = row rec_id data ,
$Bike_ID = row bike_id data,
$Station = row station data,
$Line = row line data
but i get these instead
$row rec_id data = row rec_id data ,
$row bike_id data = row bike_id data,
$row station data = row station data,
$row line data = row line data