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?

oliverbj
  • 5,771
  • 27
  • 83
  • 178
  • 1
    It doesn't work because you're using `MYSQL_NUM` but then attempting to retrieve string keys `'ac0'`. – Michael Berkowski Oct 27 '13 at 16:15
  • 5
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Oct 27 '13 at 16:17
  • Change `while ($adData = mysql_fetch_array($r, MYSQL_NUM))` to `while ($adData = mysql_fetch_assoc($r)` – Amal Murali Oct 27 '13 at 16:17
  • 1
    You ought to be using associative keys instead. But the larger issue is the glaring SQL injection vulnerability via `$_GET['token']`. New code should not be written with the old deprecated `mysql_*()` API. Instead consider switching to an API supporting prepared statements, such as PDO. – Michael Berkowski Oct 27 '13 at 16:17
  • 2
    Mmmmmm the code looks perfectly ripe for SQL injection. Hackers will approve. p/s: Try `mysqli_` or PDO. – Terry Oct 27 '13 at 16:18
  • @Terry Just switching to MySQLi or PDO doesn't magically solve SQL injection issues; let the OP switch to prepared statements, as John already suggested. – Marcel Korpel Oct 27 '13 at 16:30

2 Answers2

0

Change your numeric fetch to an associative one, then add a foreach loop to process the result.

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

Also I hope you're validating $_GET['token'] against possible mischief in your code.

Community
  • 1
  • 1
Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • 1
    No, don't validate `$_GET`, `$_POST` and `$_COOKIE` parameters, but use prepared statements, as suggested by the accepted answer on the question you linked to. – Marcel Korpel Oct 27 '13 at 16:32
0

You can create an arrays to add the values from the result query

1st is to create an array:

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

Now, to add content to 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.";
}

to call for the array values, 1 by 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++;
}

I don't know if my answer solved your question, please comment if not.

Momar
  • 177
  • 1
  • 2
  • 10