0

I am trying to get specific data from a while loop and loop it x number of times.

I'm selecting this data:

$r=mysql_query("SELECT ac6, ac5, ac4, ac3, ac2, ac1, ac0 FROM advertisements WHERE token = '".$_GET['token']."'");
while ($adData = mysql_fetch_array($r, MYSQL_NUM))
{
    $data = $adData;
    $ac0 = $data['ac0'];
    $ac1 = $data['ac0'];

print $ac0;
print $ac1;

}

This doesn't work. Nothing gets printed out.

What I want to do is to get ac6 to ac0 value for that specific advertisement (where token). How can I do that?

4

2 回答 2

0

将您的数字获取更改为关联的,然后添加一个 foreach 循环来处理结果。

$r = mysql_query("SELECT ac6, ac5, ac4, ac3, ac2, ac1, ac0
    FROM advertisements WHERE token = '" . $_GET['token'] . "'");
while ($adData = mysql_fetch_assoc($r))
{
    foreach ($adData as $key => $value)
    {
        $nubmer = (int)substr($key, 2);
        print $value; // or whatever you actually want to do
    }
}

另外,我希望您验证代码$_GET['token']中可能存在的恶作剧

于 2013-10-27T16:26:26.297 回答
0

您可以创建一个数组来添加结果查询中的值

第一个是创建一个数组:

$advert_AC0 = array();
$advert_AC1 = array();
$advert_AC2 = array();
$advert_AC3 = array();
$advert_AC4 = array();
$advert_AC5 = array();
$advert_AC6 = array();

现在,将内容添加到数组

$r = mysql_query("SELECT ac6, ac5, ac4, ac3, ac2, ac1, ac0
    FROM advertisements WHERE token = '" . $_GET['token'] . "'");
if(mysql_num_rows($r)){ //check 1st if there is num of rows from the result
 while ($adData = mysql_fetch_assoc($r))
 {
    array_push($advert_AC0, $dData['ac0']);
    array_push($advert_AC1, $dData['ac1']);
    array_push($advert_AC2, $dData['ac2']);
    array_push($advert_AC3, $dData['ac3']);
    array_push($advert_AC4, $dData['ac4']);
    array_push($advert_AC5, $dData['ac5']);
    array_push($advert_AC6, $dData['ac6']);
 }
}else{
 echo "NO RESULT.";
}

调用数组值,1 乘 1

$count_array = count($advert_AC0);
$i = $count_array;
while(1 <= $i){
 echo "AC0: $advert_AC0[$i]<br>";
 echo "AC1: $advert_AC1[$i]<br>";
 echo "AC2: $advert_AC2[$i]<br>";
 echo "AC3: $advert_AC3[$i]<br>";
 echo "AC4: $advert_AC4[$i]<br>";
 echo "AC5: $advert_AC5[$i]<br>";
 echo "AC6: $advert_AC6[$i]<br>";
 $i++;
}

不知道我的回答是否解决了你的问题,如果没有请评论。

于 2013-10-27T16:51:23.573 回答