我想知道。我总是先获取变量,然后在循环时创建变量。有没有办法更有效地做到这一点?就像自动一样?
也许像convert_to_variable("name","description","etc")之类的东西,它会自动为我设置变量,而无需手动执行每个变量?或者也许像convert_to_variable($rows)这样的单个命令,它会为我完成剩下的事情。
这就是我现在正在做的事情。
$sql = "SELECT * from projects";
$rows = $db->fetch_all_array($sql);
foreach($rows as $row) {
$name = $row['name'];
$description = $row['description'];
}
事情会更容易
$sql = "SELECT * from projects";
$rows = $db->fetch_all_array($sql);
foreach($rows as $row) {
convert_to_variable($row);
echo $name, $description;
}
最佳答案
extract可以为您做到这一点。
关于PHP + MySQL : Get result from fetch array and convert to variables automatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3965565/