Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为变量分配MySQL表值的更简单方法_Php_Mysql - Fatal编程技术网

Php 为变量分配MySQL表值的更简单方法

Php 为变量分配MySQL表值的更简单方法,php,mysql,Php,Mysql,我想知道是否有更简单的方法将我所有的不同列分配给一个变量。我有超过70个来检查用户是否获得了一首特定的歌曲。这是代码的缩短版本。我还没有完成所有的歌曲,我想知道是否有更简单的方法 $results = mysqli_query($con,"Select * FROM users WHERE user_name ='$username'"); while ($row = mysqli_fetch_array($results)) { $userpoints = $row['points']; $i

我想知道是否有更简单的方法将我所有的不同列分配给一个变量。我有超过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']; 
}

编辑:看看其他人关于使用
extract()
的例子,它与我这里的foreach循环有类似的功能:

尝试使用变量:

while ($row = mysqli_fetch_array($results))
{
    foreach ($row as $column => $value) {
        $$column = $value;
    }
}
对于每一行,实际上您将加载与列名同名的变量。这就像做:

$points = $row['points'];
$intro = $row['intro'];
$fightingfires= $row['fightingfires'];
$may = $row['may'];
$crowdedstreet= $row['crowdedstreet'];
等等

基本上,“points”是第一个键的名称,变量名为“points”,因此您可以得到变量
$points


编辑:看看其他人使用
extract()
的例子,它与我这里的foreach循环有类似的功能:

尝试使用变量:

while ($row = mysqli_fetch_array($results))
{
    foreach ($row as $column => $value) {
        $$column = $value;
    }
}
对于每一行,实际上您将加载与列名同名的变量。这就像做:

$points = $row['points'];
$intro = $row['intro'];
$fightingfires= $row['fightingfires'];
$may = $row['may'];
$crowdedstreet= $row['crowdedstreet'];
等等

基本上,“points”是第一个键的名称,变量名为“points”,因此您可以得到变量
$points


这就像做一件事一样简单

extract($row);
你的循环看起来像

while ($row = mysqli_fetch_array($results))
{
    extract($row);
    echo $intro;
}

这就像做一件事一样简单

extract($row);
你的循环看起来像

while ($row = mysqli_fetch_array($results))
{
    extract($row);
    echo $intro;
}

是的,使用php extract()

下面是一个简单的例子:

$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.
是的,使用php extract()

下面是一个简单的例子:

$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.

清楚地解释你的问题。清楚地解释你的问题。它起作用了谢谢,我的代码中对结果有不同的名称,所以它最初不起作用,你只是节省了我这么多时间,特别是当我扩展我的目录时,我得到了两个工作答案,你先回答,我能接受两个答案吗,它是如何工作的?我是新来的site@MichaelStClair我绝对不会拒绝对我的答案投反对票,尽管我认为乔登的答案比我的更优雅,所以你应该接受那个人的答案(每个问题你只能接受一个答案)。但是谢谢乔登的教育!它成功了谢谢,我的代码中有一个不同的结果名称,这就是为什么它最初不起作用的原因,你为我节省了很多时间,特别是当我扩展我的目录时,我得到了两个工作答案,你先回答,我能接受两个答案吗,它是如何工作的?我是新来的site@MichaelStClair我绝对不会拒绝对我的答案投反对票,尽管我认为乔登的答案比我的更优雅,所以你应该接受那个人的答案(每个问题你只能接受一个答案)。但是谢谢乔登的教育!