0

So here is my PHP code:

$checkProject1Column = "SELECT * FROM tbl_studentchoice WHERE Project1 = '1'";
$resultProject1Column = mysqli_query($conn, $checkProject1Column);       
    while($row = mysqli_fetch_assoc($resultProject1Column)) {
            //Create an array of students
            $array[] = ['studentid' => $row['studentid']];
    }  
    //check marks database
    $checkmarks = "SELECT studentid,marks FROM tbl_marks";
    $checkResult = mysqli_query($conn, $checkmarks);
    if ($checkResult->num_rows >= 0){
        while($row1 = mysqli_fetch_assoc($checkResult)){
            //Create an array of students
            $array1[] = [
                'marks' => $row1['marks'],
                'studentid' => $row1['studentid']
                ];
            }
        print_r($array);
        print_r($array1);

So I have 2 database tables. tbl_studentchoice and tbl_marks. I created an array of students with a value 1 on Project1 column. This is how my tbl_studentchoice looks like:

projtbl.

So now that I have an array of studentid's. I want to get the marks of these students from another table (tbl_marks) which looks like this:

marks

and create a new array with only studentid with Project1 value 1 and their marks. So basically I want to create an array like this:

Array ( 
    [0] => Array ( 
        [studentid] => c12558111 
        [marks] => 50
    ) 
    [1] => Array ( 
        [studentid] => c12558112 
        [marks] => 60
    ) 
)
2
  • Is there a reason for the duplicate $resultProject1Column = mysqli_query($conn, $checkProject1Column); ? Commented May 1, 2016 at 16:53
  • Actually no let me edit my code Commented May 1, 2016 at 16:55

1 Answer 1

4

Why are you using two diffrent query?? you should join tables only write one query like this

select s.*,m.marks
  from tbl_studentchoice s, tbl_marks m
  where s.studentid = m.studentid
   and  Project1 = '1'

Complete Code

$query= "select s.*,m.marks
  from tbl_studentchoice s, tbl_marks m
  where s.studentid = m.studentid
   and  Project1 = '1'";
$result= mysqli_query($conn, $query);       
while($row = mysqli_fetch_assoc($result)) {

  echo("<br/>Student ID = ".$row['studentid']);
  echo("<br/>Student marks = ".$row['marks']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I still do not have enough experience on PHP so I didn't know you could do a query like that. Can you explain the query to me I do not fully understand it. I have done and created the array and its good. I just want to understand that query a bit more.
It is like a inner join which joins two table with some condition, in your problem there were two table with one common column which is studentid so this query will merge data of two table whose studentid is same

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.