PHP - not sure how to pull info from database and set into var line

I have some entries in a database.
I pull from the database and just want to use the "id" numbers

For example:  in the database i have 4 entries
so I will get the follow when pulling all the rows:  "1 2 3 4"

I want to insert these so that it appears like the following in the code:

$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];

My best guess was:  $q.$row->id = $_POST['q'.$row->id];

But I  KNOW  this is wrong.
Can someone correct it?
Who is Participating?
Cornelia YoderArtistCommented:
//connect to database
$result = mysql_query("SELECT q1, q2, q3, q4 FROM Table");
$row = mysql_fetch_array($result);

innercproductionsAuthor Commented:
Oh ok.
I see that I didn't make one point clear.

It will not ALWAYS be 1 2 3 and 4.
The numbers will change... and so I need this code to be dynamic.
What ever the numbers are in the db... is what these need to be.

Here's more code:

$query = "SELECT * FROM $questionstable_name WHERE companynumber = '$carriedcompanynumber' AND questionstatus = '$questionstatus_active' ORDER BY questionadddate";
$result = mysql_query($query) or custom_die($database_errorinquery);

if(mysql_num_rows($result) > 0) {       while($row = mysql_fetch_object($result)) {

$q.$row->id = $_POST['q'.$row->id];

( I need JUST the last line changed if possible. )

Use arrays.

$q = array();
$q[$row->id] = $_POST['q' . $row->id];

you can then iterate through it with a foreach loop, which i am sure you will want to do later

foreach ($q as $key => $elem) {
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

innercproductionsAuthor Commented:
Arrays are something that I kind of get...kind of don't.
In short, I know a little about them, but not enough to use them outside of copy and paste.
Same goes with foreach loop.

Is it possible to show me the code in use?

I just want this "array" that you're talking about to let me dynamically create q1, q2, and any other q(number) that exists in the database...
It might be q1, q7, q8, q24... and there might be 20 of them... or just two...
But whatever number exist in the database... then create a variable for them in the format of "q1"

Can someone expand on the code?
Well i am sorry to say this, but these variables that you speak of are quite an advanced topic.
they are called variable variables, and can pose a security threat if used improperly... but anyway...

they can be achieved like this:

${"q" . $row->id} = $_POST["q" . $row->id];

I still would reccomend arrays as it will be impossible to iterate through these dynamically created variables.
innercproductionsAuthor Commented:
Thank you. I will be back on this in the next day and attempt to close out the thread and reward points.
That is, if I work it out.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.