如果添加了新查询,如何获取

时间:2011-09-21 07:03:11

标签: php mysql

while($fetchres = mysql_fetch_array($searchquery)) { 
        $v1 = $fetchres['v1']; $v2 = $fetchres['v2']; $v3 = $fetchres['v3']; 
        $v4 = $fetchres['v4']; $v5 = $fetchres['v5'];
        $v6 = $fetchres['v6']; 
        $v7 = $fetchres['v7'];
        }

嗨!如何自动设置此列?我的意思是我的系统会自动添加一列。例如,我的系统添加了一个新列'v8'。但在我的代码中,我只输入到v7。如何根据系统自动生成的列数自动编写查询提取的代码?感谢。

4 个答案:

答案 0 :(得分:1)

如果您真的想这样做,可以使用extract($fetchres)

有关extract

的更多信息

示例:

$fetchres = array('v1'=>'foo', 'v2'=>'bar');
extract($fetchres);
echo '$v1 is ' . $v1 . 'and $v2 is ' . $v2;

答案 1 :(得分:1)

使用extract

while ($row = mysql_fetch_assoc($searchQuery))
{
  // Extract the keys of the array into the local symbol table.
  extract($row);

  // Now, all variables exist and you can echo them like this:
  echo $v1;
  echo $v8;
}

您仍然需要知道变量的名称。如果您只想回显内部的所有内容,请使用带有键任务的foreach:

答案 2 :(得分:0)

你可以使用variable variables,虽然我认为这是一个非常糟糕的主意。

答案 3 :(得分:0)

我认为你真的想要这个:

while ($fetchres = mysql_fetch_array($searchquery)) {
    foreach ($fetchres as $key => $value) {
        echo $key . ': ' . $value . PHP_EOL;
    }
}