I got a problem to create SQL statement in calling two tables in the same database. I'm trying to call this two tables to view both of the table's content when i'm doing the search process in my system. For example, i have QUESTION field in table Question, and field A, B, C, and D in table Answer. So i would like to call both table to be displayed when i'm doing the search proses. Anybody could help me in making the SQL statement?
how to connect two tables in the same database
Collapse
X
-
Tags: None
-
Originally posted by naughtybynatureI got a problem to create SQL statement in calling two tables in the same database. I'm trying to call this two tables to view both of the table's content when i'm doing the search process in my system. For example, i have QUESTION field in table Question, and field A, B, C, and D in table Answer. So i would like to call both table to be displayed when i'm doing the search proses. Anybody could help me in making the SQL statement?
Note:where a.question_id is the primary key of question table and b.question_id is the foreign key in the answer table. -
<html>
<head>
<title>Search Questions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$query = '';
$query2 = '';
$searchtype=$HT TP_POST_VARS['searchtype'];
$searchterm=$HT TP_POST_VARS['searchterm'];
$searchterm=tri m($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'You haven\'t entered search details. Please try again';
exit;
}
$searchtype = addslashes($sea rchtype);
$searchterm = addslashes($sea rchterm);
@ $db = mysql_pconnect( 'localhost', 'root', '');
if (!$db)
{
echo 'Error: Cannot connect to database. ;p';
exit;
}
mysql_select_db ('sistem bank soalan');
$query = "select * from soalan where ".$searchty pe." like '%".$searchterm ."%'" ;
$query2 = "select * from jawapan where ".$searchty pe." like '%".$searchterm ."%'";
$result = mysql_query($qu ery);
$result2 = mysql_query($qu ery2);
//i'm trying to call both table using this statement. i know it was wrong but i just don't know how to solve this problem.
$num_results = mysql_num_rows( $result && $result2);
echo '<p>Number of question found: '.$num_results. '</p>';
for ($i=0; $i <$num_results ; $i++)
{
$row = mysql_fetch_arr ay($result);
//from table soalan
echo '<p><strong>'.( $i+1).'. No Soalan: ';
echo htmlspecialchar s(stripslashes( $row['id']));
echo'</strong><br />Soalan: ';
echo stripslashes($r ow['soalan']);
echo '<br />A: ';
//from table jawapan
echo stripslashes($r ow['jwpA']);
echo '<br />B: ';
echo stripslashes($r ow['jwpB']);
echo '<br />C: ';
echo stripslashes($r ow['jwpC']);
echo '<br />D: ';
echo stripslashes($r ow['jwpD']);
echo '<br />Jawapan: ';
echo stripslashes($r ow['jwpObj']);
//from table soalan
echo '<br />Aras Kesukaran: ';
echo stripslashes($r ow['araskesukaran']);
echo '<br />Sesi Peperiksaan: ';
echo stripslashes($r ow['sesipeperiksaa n']);
echo '<br />Bahagian: ';
echo stripslashes($r ow['bahagian']);
echo '<br />Add User: ';
echo stripslashes($r ow['adduser']);
echo '</p>';
}
?>
<p align="center"> <font face="BatangChe "><strong> </strong></font></p>
</body>
</html>
NOTES: i've two tables in this database which are table QUESTION and ANSWER. the primary key of both tables are the same; which is id. but i can't combine both tables because of some reason. so when i'm doing the seach process; i mean search engine for my system; i found a problem where i can't call both table at the same time to display the content. Refering to the same primary key; id; i want to call both table to get their content. Anyone could help me with this?Comment
-
Originally posted by raji20select a.*, b.* from Question a LEFT JOIN Answer b ON a.question_id=b .question_id
Note:where a.question_id is the primary key of question table and b.question_id is the foreign key in the answer table.Comment
Comment