Foreach循环声明不同的变量取决于select列

时间:2015-08-27 01:04:52

标签: php sql-server foreach

参考:Triangulation

好的......没有更多的语法错误

同样的问题

  • 是否可以在foreach循环中创建变量,每个变量名都取决于我选择的列名?

我的编码类似

$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";
}

结果应为

$Rec_ID = row rec_id data , 
$Bike_ID = row bike_id data, 
$Station = row station data, 
$Line = row line data

但是我得到了这些

$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

0 个答案:

没有答案