473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to connect two tables in the same database

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?
Sep 11 '06 #1
3 12399
raji20
28
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?
select 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.
Sep 11 '06 #2
<html>
<head>
<title>Search Questions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php

$query = '';
$query2 = '';

$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm=trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You haven\'t entered search details. Please try again';
exit;
}

$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $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 ".$searchtype." like '%".$searchterm."%'" ;
$query2 = "select * from jawapan where ".$searchtype." like '%".$searchterm."%'";

$result = mysql_query($query);
$result2 = mysql_query($query2);

//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_array($result);
//from table soalan
echo '<p><strong>'.($i+1).'. No Soalan: ';
echo htmlspecialchars(stripslashes($row['id']));
echo'</strong><br />Soalan: ';
echo stripslashes($row['soalan']);
echo '<br />A: ';
//from table jawapan
echo stripslashes($row['jwpA']);
echo '<br />B: ';
echo stripslashes($row['jwpB']);
echo '<br />C: ';
echo stripslashes($row['jwpC']);
echo '<br />D: ';
echo stripslashes($row['jwpD']);
echo '<br />Jawapan: ';
echo stripslashes($row['jwpObj']);
//from table soalan
echo '<br />Aras Kesukaran: ';
echo stripslashes($row['araskesukaran']);
echo '<br />Sesi Peperiksaan: ';
echo stripslashes($row['sesipeperiksaan']);
echo '<br />Bahagian: ';
echo stripslashes($row['bahagian']);
echo '<br />Add User: ';
echo stripslashes($row['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?
Sep 11 '06 #3
select 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.
nway, could you please explain your SQL statement by refering to my coding that i've post just now?
Sep 11 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: atlasyeo | last post by:
Hi, my first time posting on a newsgroup. anyway, let's cut to the chase. I'm trying to migrate mysql database form one server to another server. So I copied the data from /var/lib/mysql to the...
4
by: Wonderinguy | last post by:
Our websphere application uses a generic application userid to connect and query db2 on z/os via DB2 connect. The end user,logs in to the application using his regular userid, which is then...
3
by: Fabian Knopf | last post by:
Hi friends, i have a unix machine PC1 where a database is running ( IBM DB2 V8.1 ) . Then i have another machine PC2 i installed there also ( IBM DB2 V8.1 ). On PC2 i installed unixODBC. To...
1
by: XML newbie: Urgent pls help! | last post by:
I am working on Vb.Net application. I have to connect to remote server(it's IP address)(this server is not on my network). This company has provide me username, companyname and password. Once, I...
5
by: rAinDeEr | last post by:
Hi, Y'day one of my friend showed me how to connect to a Oracle database as SYSDBA, DBA etc I have DB2 UDB installed in Linux machine.Right now, I have a user WADI which has got some...
1
by: Jason Kester | last post by:
I have an application that creates a new database during installation, and I've noticed some strange behavior. Once I've created the database, I am able to immediately create tables and populate...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
6
by: Al G | last post by:
Can someone tell me what I need to get connected to our AS400? I am trying to write an app in VS2005(Data source, Gridview) that requires data from files on our AS400. I've downloaded DB2, and...
15
Zwoker
by: Zwoker | last post by:
Hi all, I am accessing data from an Oracle database via a connect string. I got into the situation where I wanted to write my SQL select statement to do a join between a local table sitting in...
4
by: psyvanz | last post by:
Im a college student, my program is a computerized enrollment system, Im using MS ACCESS database in connecting my VB6 program form. Inside i'd created two tables, named "tbstudentinfo" and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.