将MySQL表值分配给变量的更简单方法

Mic*_*air 1 php mysql

我想知道是否有更简单的方法将所有不同的列分配给变量.我有超过70来检查用户是否已获得特定歌曲.这是代码的缩短版本.我还没有完成所有的歌曲,我想知道是否有更简单的方法.

$results = mysqli_query($con,"Select * FROM users WHERE user_name ='$username'");
while ($row = mysqli_fetch_array($results))
{
$userpoints = $row['points'];
$intro = $row['intro'];
$fightingfires = $row['fightingfires'];
$may = $row['may'];
$crowdedstreet = $row['crowdedstreet']; 
}
Run Code Online (Sandbox Code Playgroud)

小智 6

是的,使用php extract()http://php.net/manual/en/function.extract.php

这是一个简单的例子:

$results = mysqli_query($con,"Select * FROM users WHERE user_name ='$username'");
while ($row = mysqli_fetch_array($results))
{
extract($row);
}
// This will give you variables for each item
// so you will get $points for what would have been $row['points'], etc.
Run Code Online (Sandbox Code Playgroud)