0

So I'm running a stored procedure that returns several rows, each row containing a PK counter and a phone number. I'm in need of a way to 'automate' the creation of variable names and assign those values to them. If I can't automate it, I at least need to create 3 different variables inside the while loop.

$rs = mysql_query("CALL GetUserNumbers('" . $_SESSION['UserID'] . "')");
    while ($row = mysql_fetch_assoc($rs))
    {
    $incrementingvariablename = row['phonenumber'];
    // something like $_1 = firstrow, $_2 = secondrow etc until all the rows have been read.
    }

Thanks in advance!

2 Answers 2

4

arrays are good for this:

$incrementingvariablename[] = row['phonenumber'];

or

$incrementingvariablename[] = array('phone'=>row['phonenumber'],'counter'=>row['counter']);
Sign up to request clarification or add additional context in comments.

Comments

0

How about using this php feature:

$foo = 'bar';
$$foo = 'Hello I am bar';

echo $bar; // Outputs 'Hello I am bar';
echo "\n";

4 Comments

This is just an extremely messy alternative to arrays.
Yeah, but it is what question author asks: how to dynamically create variables.
This is useful to know. Thanks a bunch. I wonder if it would work for values pulled from a query result set? even so though, the array method seems much more feasible.
In fact, the down vote is not fair: the answer is indeed correct given the question (but it was someone else who voted so). @Kulingar: result sets are by no means a special case; they come in variables as well.

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.