Aaron M.
Aaron M.

Reputation: 749

Run a separate query for each ID found in the array

Trying to run a second query for items in an array. First I've created an array of user information, then for each of the users I need to query for their answers found in the answers table.

The idea is for the array to have data from the first query and then simply add the data from the answers to the related user element. The code below is only listing the first system.

I've been so reluctant to post my question here, but my entire day has been consumed by this and I'm getting no where fast. Apologies in advance for my obviously flawed approach.

 Array
(
    [0] => Array
        (
            [fname] => asdf
            [lname] => asdf
            [minitial] => a
            [rank] => MAJ
            [uniq] => !s5$qn

            [sysName] = System 1 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom

            [sysName] = System 2 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom

            [sysName] = System 3 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom


   [1] => Array
        (
            [fname] => asdf
            [lname] => lkjlkj
            [minitial] => i
            [rank] => oiuoi
            [uniq] => @z26dr

            [sysName] = System 1 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom

            [sysName] = System 2 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom

            [sysName] = System 3 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom


        )

//CODE

$sql = "SELECT fname, lname, minitial, rank, uniq FROM `user` join answers on answers.uniqid = user.uniq";
$data = mysqli_query($con, $sql) or die("MySQL ERROR: ". mysqli_error($con));

$users = array();
$i = 0;

while ($row = mysqli_fetch_array($data, MYSQL_ASSOC))
{
    $users['answers'][$i] = array (
        "fname" => $row['fname'], 
        "lname" => $row['lname'], 
        "minitial" => $row['minitial'], 
        "rank" => $row['rank'], 
        "uniq" => $row['uniq']
     );

    $query2 = "SELECT a.sysid, s.sysName, uniqid, choice, priority, termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";
    $data2 = mysqli_query($con, $query2);

    while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
    {       
        $users['answers'][$i]['sysName'] = $row2[1];
        $users['answers'][$i]['choice'] = $row2[3];     
    }

    $i++;
}

Thank you in advance for any insight you might share.

EDIT: This is the array coming back, and only the first system is being listed for each user.

[2] => Array
        (
            [fname] => asdf
            [lname] => lkjlkj
            [minitial] => i
            [rank] => oiuoi
            [uniq] => @z26dr
            [sysName] => Super Terminate System
        )

    [3] => Array
        (
            [fname] => Juuu
            [lname] => kjuuu
            [minitial] => k
            [rank] => LTC
            [uniq] => gthdz%
            [sysName] => Super Terminate System
        )

Upvotes: 0

Views: 248

Answers (1)

pixelsucht
pixelsucht

Reputation: 228

O.K. you don't exactly tell what happens when you run your script (What is the Problem). But when i look at your coude i can assume what is going wrong.

First of all, I think you could also do it with on query:

$sql = "SELECT * FROM `user` 
        join answers on answers.uniqid = user.uniq
        LEFT JOIN systems s ON s.sysID = a.sysid";

Hopefully this helps. Otherwise:

In your second query there is a problem. I think you should change:

$query2 = "SELECT a.sysid, s.sysName, uniqid, choice, priority, termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";

to

$query2 = "SELECT a.sysid, s.sysName, s.uniqid, s.choice, s.priority, s.termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";

The s. before choice = s.choice

Upvotes: 1

Related Questions