Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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?
  • 3
  • 2
2 Solutions
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) {
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now