1

So i have the following code that works and outputs the information let's say 2 11. I would like to have $keyidcall increment in variable name so $keyidcall2, $keyidcall3....... with the additional variables I would like the variable to hold the correct value so I can call it later. so expected output would be $Apiammount ="2"; echo $keyidcall; would echo 2 echo $keyidcall: would echo 11

while($Apiammount > 1){
    $Keyidquery = mysqli_query($connection, "SELECT ID FROM `Characterapi` WHERE UserId = '$Idcall'");
    while($keyid = mysqli_fetch_assoc($Keyidquery)){
        $keyidcall = $keyid['ID'];
        echo $keyidcall;
    }

    $Apiammount--;
}
4
  • 1
    Any time you find yourself using numbered variables like that, you almost always should use an array instead. Commented Feb 19, 2016 at 22:02
  • @Barmar I don't think I know of any need for it Commented Feb 19, 2016 at 22:04
  • Neither do I, but I try to avoid absolutes. Commented Feb 19, 2016 at 22:04
  • Looking at your code, the $Keyidquery will be the same all the time, since '$Idcall' is not changed anywhere in the loop. If you want to return the same value and store it in multiple variables there is no need to run the query over and over again. Once you got that sorted, as @Adam Copley suggested, store them in array. Commented Feb 19, 2016 at 22:07

2 Answers 2

1

The better way to do this would be to store the values in an array.

$keyidcall[] = $keyid['ID'];

Then you can refer to them later as

echo $keyidcall[0]; echo $keyidcall[1];

in the order that they were entered in.

Or if you wanted something more specific to refer to it by, you could use

$keyidcall[$Apiammount] = $keyid['ID'];

then you would refer to them as:

echo $keyidcall[<apiamount>];

Assuming you know what that would be.

Sign up to request clarification or add additional context in comments.

3 Comments

So after realiising i needed to create the array this worked AMAYAZINGLY. TY
now is there a way i can use the $keyidcall[] in a msqli query where i can do upon $keyidcall[0] = 3 in a my msqli query the call uses the three and i can loop it ?
I recommend you post a separate question and link in these comments. What your asking needs a little more detail
0

You can do:

$i = 0;
while($foo){
  $name = 'keyidcall';
  $i++;
  $newvar = $name . $i;
  echo $$newvar;
}

And $$newvar will echo the value of "keyidcallX" where X is an incrementing value. Not sure if that's what you meant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.