0

I have 2 table names courses and institution. Here is my query:

CREATE TEMPORARY TABLE 
IF NOT EXISTS temp  AS (SELECT course_name, ins_name
         FROM courses 
         where course_name='ACCA') ;

select institution.ins_name, address, contact, website, 
email from institution join temp on institution.ins_name = temp.ins_name

And here is my php script:

<?php
session_start();
include('db_connect.php');
$sql="CREATE TEMPORARY TABLE 
IF NOT EXISTS temp  AS (SELECT course_name, ins_name
         FROM courses 
         where course_name='ACCA') ;

 select institution.ins_name, address, contact, website, email from institution join temp on institution.ins_name = temp.ins_name";

$result= mysqli_query($conn, $sql) or die("MySQL error: " . mysqli_error($conn)); 

while ($row = mysqli_fetch_array($result)){

echo "<tr>";
echo "<td>" . $row['ins_name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['email'] . "</td>";

echo "</tr>";
}
echo "</table>"

?>

I can't figure out what the problem is. The error i get is:

MySQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select institution.ins_name, address, contact, website, email from institution j' at line 6

What am i doing wrong?

3 Answers 3

2

From Multiple Statements:

MySQL optionally allows having multiple statements in one statement string. Sending multiple statements at once reduces client-server round trips but requires special handling.

Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.

Alternatively, just fire two queries.

Sign up to request clarification or add additional context in comments.

Comments

1

Every query must be executed separately. create and select are two different queries so first you need to call mysqli_query for create and then mysqli_query for select

1 Comment

got it. had to query them separately. Thank you sir
0

You need alias on each column :

 select institution.ins_name, institution.address, institution.contact, institution.website, institution.email from institution join temp on institution.ins_name = temp.ins_name";

Comments

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.