Anthony Galpo Anthony Galpo - 7 months ago 9
PHP Question

How to use array data into query to get another data in PHP

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

Answer

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']);
}
Comments