开发者

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

开发者 https://www.devze.com 2023-01-21 10:24 出处:网络
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?

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

精彩评论

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