开发者

PHP + MySQL: Get result from fetch array and convert to variables automatically?

I was wondering. I always do fetch and then create variables when I do loop. Is there a way to do this more efficient? Like as in automatically?

Maybe something like convert_to_variable("name","description","etc") and it'll automatically set variables for me without having to do each manually? Or maybe a single command like convert_to_variable($rows) and it'll do the rest for me.

Here is what I am doing now.

$sql = "SELECT * from projects";
$rows = $db->fetch_all_array($sql);
 foreach($rows as $row) {
  $name = $row['name'];
  $description = $row['description'];
}

Something easier would be

$sql = "SELECT * from projects";
$rows = $开发者_如何学运维db->fetch_all_array($sql);
 foreach($rows as $row) {
  convert_to_variable($row);
  echo $name, $description;
}


extract can do that for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜