0

I use this code it run very good but the problem how I can execute the second query:

$query .= "SELECT * FROM `course` where id = 201102887;"; 

which is from another table.The first query work fine . Can you help me or advise me to another way to run many query .

<?php
$link = mysqli_connect("localhost", "root", "", "uoh");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT * FROM `student_record` where id = 201102887;";
$query .= "SELECT * FROM `course` where id = 201102887;"; 

/* execute multi query */
if (mysqli_multi_query($link, $query)) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link)) {
            while ($row = mysqli_fetch_row($result)) {
                printf("%s\n", $row[0]);
                printf("%s\n", $row[1]);

            }
            mysqli_free_result($result);
        }

        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}


/* close connection */
mysqli_close($link);
?>
ajshort
  • 3,684
  • 5
  • 29
  • 43
Farhan
  • 21
  • 1
  • 1
  • 4

1 Answers1

-1

Try UNION for this

<?php
$link = mysqli_connect("localhost", "root", "", "uoh");
$query = "SELECT * FROM `student_record` where id = 201102887 UNION SELECT * FROM `course` where id = 201102887";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_row($result))
{
    print_r($row);
}
?>

This should print two arrays (one per row), one from the student_record table (assuming id is unique) and one from the course table assuming that id is unique.

Matt
  • 2,851
  • 1
  • 13
  • 27