473,395 Members | 1,653 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.

connect two tables on php

7
I got two tables on a database but I dont know how to run independently. All I want is each table to run independently once tracked.
the first table is doing ok, but i dont know how to put the second table in the coding.

<?php
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("maklumatpesakit");

$term = $_POST['term'];

$sql = mysql_query("select * from main where NoID like '%$term%'");

while ($row = mysql_fetch_array($sql)){
echo 'ID: '.$row['NoID'];
echo '<br/> Name: '.$row['Name'];
echo '<br/> Age: '.$row['Age'];
echo '<br/> Date Of Birth: '.$row['DateOfBirth'];
echo '<br/> Sex: '.$row['Sex'];
echo '<br/> Race: '.$row['Race'];
echo '<br/> Status: '.$row['Status'];
echo '<br/> Address: '.$row['Address'];
echo '<br/> Contact No: '.$row['NoTel'];
echo '<br/> Religion: '.$row['Religion'];
echo '<br/> Mode of Transport: '.$row['MobPatient'];
echo '<br/> Patient Mobility: '.$row['PatientMob'];
echo '<br/> Reference: '.$row['Reference'];
echo '<br/><br/>';
}
?>


my second table is 'reference', where the echo show the data from the table reference in my database. i just want to call from the table to show the data in echo but i cant figure how to do this.
pls help.
Feb 22 '11 #1
8 2679
johny10151981
1,059 1GB
before echoing retrieve all data and then echo.

If you do any process before your data wont lost or any other trouble wont be generated
Feb 22 '11 #2
howie
7
im still new to this thing.. can u give code example for retrieving data?
Feb 22 '11 #3
johny10151981
1,059 1GB
You already did that
Feb 22 '11 #4
Can you nplease provide 'reference' table structure?
What all columns of 'reference' table you want to show?

You need to join the two tables.

select * from main,reference where NoID like '%$term%'

this SQL statement show all data from the two tables.

Is 'Reference' is a column of 'reference' table??



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect ("localhost", "root","") or die (mysql_error());
  3. mysql_select_db ("maklumatpesakit");
  4.  
  5. $term = $_POST['term'];
  6.  
  7. $sql = mysql_query("select * from main,reference where NoID like '%$term%'");
  8.  
  9. while ($row = mysql_fetch_array($sql)){
  10. echo 'ID: '.$row['NoID'];
  11. echo '<br/> Name: '.$row['Name'];
  12. echo '<br/> Age: '.$row['Age'];
  13. echo '<br/> Date Of Birth: '.$row['DateOfBirth'];
  14. echo '<br/> Sex: '.$row['Sex'];
  15. echo '<br/> Race: '.$row['Race'];
  16. echo '<br/> Status: '.$row['Status'];
  17. echo '<br/> Address: '.$row['Address'];
  18. echo '<br/> Contact No: '.$row['NoTel'];
  19. echo '<br/> Religion: '.$row['Religion'];
  20. echo '<br/> Mode of Transport: '.$row['MobPatient'];
  21. echo '<br/> Patient Mobility: '.$row['PatientMob'];
  22. echo '<br/> Reference: '.$row['Reference'];
  23. echo '<br/><br/>';
  24. }
  25. ?>
  26.  
Feb 22 '11 #5
howie
7
i have try the:
select * from main,reference where NoID like '%$term%'
and it show an error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\MaklumatPesakit\search.php on line 9

ok, i have two tables 'main' and 'reference', and in 'main' i have a reference field where i hope to connect to 'reference' table to show the data. is it possible?

or i just have to delete the reference field and connect the NoID from 'main' and 'reference' table. but how can i done this?
Feb 23 '11 #6
If 'NoID' this column exists in both tables then try

Expand|Select|Wrap|Line Numbers
  1. $sql = mysql_query("select * from main,reference where main.NoID=reference.NoID and main.NoID like '%$term%'"); 
Feb 23 '11 #7
nathj
938 Expert 512MB
It seems to me that you are after the syntax for a JOIN on the tables. Have a look at this page in the MySQL guide

So If you have 2 tables - main and reference and main has an ID field and reference has a mainID field which is a foreign key then you can do something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT a.*, b.* from main a left join reference b on b.mainID = a.id where a.NoID like '%$term%'
  2.  
So there you have it, hopefully the guide and the snippet will be of some help to you.

Cheers
nathj
Feb 24 '11 #8
howie
7
thank!! now its doing good.. thanks for the info all.. ^^
Feb 24 '11 #9

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

Similar topics

3
by: Tym | last post by:
OK - daft question of the day time... If I have database A which contains all the live data, and Database B which contains linked tables to those is A (i.e. a front end) is there a way of seeing...
6
by: Don Leverton | last post by:
Hi All, I've got a situation where I am developing an Access 97 app for a client, and am in the "beta testing" stage. I have split the app up, using the DB splitter, into front-end /back-end...
6
by: Lewis Veale | last post by:
I have an Access 2000 front-end pointing at a SQL Server backend, with around 80 linked tables and views. I frequently need to point the front-end at different versions of the back-end, and achieve...
4
by: Oceania | last post by:
Hi All, I did try using the sample code from the ADO.net forum to link one table, it is working fine. But, I got a problem when I tried to link all tables (20 tables)... Please help, thank...
2
by: Randy | last post by:
I am trying to relink some Oracle tables in an Access database via VB.NET and ADOX. I receive the following error when executing the cat.ActiveConnection link "Arguments are of the wrong type,...
25
by: bubbles | last post by:
Using Access 2003 front-end, with SQL Server 2005 backend. I need to make the front-end application automatically refresh the linked SQL Server tables. New tables will be added dynamically in...
5
Luigi
by: Luigi | last post by:
I use Access 2003 on Windows XP OS. I made an Access application with many linked table (essentialy txt file but also table of other mdb files); how can I move my principal mdb file and its relavant...
0
by: jrhowcroft | last post by:
I have a number of databases in MS Access 97. Some tables in these databases are linked to ODBC tables in Oracle9/10. We would like to change the user name and password on the Oracle tables so each...
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...
16
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.