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

Posted on 2007-08-09
Last Modified: 2013-11-05
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?
Question by:innercproductions
    LVL 27

    Accepted Solution

    //connect to database
    $result = mysql_query("SELECT q1, q2, q3, q4 FROM Table");
    $row = mysql_fetch_array($result);


    Author Comment

    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. )

    LVL 3

    Assisted Solution

    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) {

    Author Comment

    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?
    LVL 3

    Expert Comment

    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.

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now