Welcome Guest ( Log In | Register )



3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> MySQL, Multiple Tables
nightfox
post Jul 25 2006, 02:24 AM
Post #1


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



Ok, I'm coding a project which is a leap than what I'd normally do. Before, I've always learned ONE table... put EVERYTHING in one database table.

I'm making a profile system so there needs to be at least two tables: 1 for users, 1 for content. My problem is, how do I link the two together? I could probably figure this out faster if someone explained and posted sample SQL code that shows how the two are linked together.

Thanks!!

[N]F
Go to the top of the page
 
+Quote Post
Vyoma
post Jul 25 2006, 04:23 AM
Post #2


Cosmic Overlord
Group Icon

Group: Members
Posts: 550
Joined: 26-November 05
From: Chennai, India
Member No.: 9,811



There are multiple ways of linking two tables, and it would depend on what exactly do you want to link these two tables to choose between them.

For most of the cases a JOIN of the tables works and in some complex cases one would require to go for INNER QUERIES. Note that if you are planning on using MySQL as your database, as far as my knowledge goes, the present version does not support INNER QUERIES. (People, correct me if I am wrong here).

Now what are JOINs? Well, ther are many different types of JOINS: Self Joins, Natural Joins, Inner and Outer Joins. Explaining them would take a lot of verbiage tongue.gif and instead I will just show a sample of JOIN query between two tables. (You may extend it to multiple tables.

Now, I am assuming here that you have two table 'users' and 'content'.

I am putting here some of the columns that I think may be present in these tables:

users
-userid (PK)
-name
-password

content
-contentid (PK)
-userid (FK)
-contenttext

The above columns are minimal, and you may need to add more. But for the sake of explaining JOINs, these should be sufficient.

Now, assume that data if filled in both the tables. The ones in 'users' table is straight forward. The data is put in 'content' table with 'userid' being a foreign key. This means, the content's author is that particular user.
userid and contentid are Primary Keys (hence PK), and userid in 'content' table is a Foreign Key (FK).

Now, we will see the SQL of how to retrive a particular content specified by contentid, along with the author name.
CODE

SELECT c.contentid, c.contenttext, u.name
FROM content c, users u
WHERE c.contentid = $cntntid
AND c.userid = u.userid


Note that c.contentid = $cntid part in the WHERE clause is like you would use in retrieving data from one table. ($cntid is just a PHP variable - you may replace it by any value, that is hard code it or pass it by any other means).

Now, what compromises of a join? Yes, the c.userid = u.userid joins these two tables. The result of the query is that it will fetch content id and content text is picked up from 'content' table and author name is picked up from 'users' table.


I have assumed a lot many things above, but if you want something specific, please do ask.
Go to the top of the page
 
+Quote Post
vhortex
post Jul 25 2006, 05:36 PM
Post #3


Guilty Until Proven Innocent
Group Icon

Group: Members
Posts: 372
Joined: 13-April 05
Member No.: 3,937



Vyoma is correct..

I know of atleast 30 ways to connect the data together and they work on different versions of mySQL.. some are version specific and some are not..

what the post above shows is the basic way of doing stuffs with connecting data.

--

if you cant understand how joins work.. i can teach you how to do the same thing without the join command but this will be a little longer query.

the sample table structure will remain the same since it is a good basic sample for your current needs.
Go to the top of the page
 
+Quote Post
yordan
post Jul 25 2006, 10:41 PM
Post #4


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 1,980
Joined: 16-August 05
Member No.: 7,896



It seems that nightfox starts learning what is a relationnal database, and why using relations is more interesting than using single tables.
Go to the top of the page
 
+Quote Post
Quatrux
post Jul 26 2006, 02:51 AM
Post #5


the Q
Group Icon

Group: [HOSTED]
Posts: 1,017
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



QUOTE(yordan @ Jul 26 2006, 01:41 AM) *

It seems that nightfox starts learning what is a relationnal database, and why using relations is more interesting than using single tables.


Yeah, agreed, that is the "fun" way to create something like this, but for me sometimes it gives a big headache how to do it "the best way".. I used to do databases with text files making them work by directories and file names and text inside, so you can imagine how my things got a lot more easier when I started getting into MySQL. wink.gif
Go to the top of the page
 
+Quote Post
nightfox
post Jul 26 2006, 03:13 AM
Post #6


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



QUOTE(Vyoma @ Jul 25 2006, 12:23 AM) *

There are multiple ways of linking two tables, and it would depend on what exactly do you want to link these two tables to choose between them.

For most of the cases a JOIN of the tables works and in some complex cases one would require to go for INNER QUERIES. Note that if you are planning on using MySQL as your database, as far as my knowledge goes, the present version does not support INNER QUERIES. (People, correct me if I am wrong here).

Wow, that's a lot! tongue.gif I'll have to find some time to read it all and absorb it! lol

[N]F
Go to the top of the page
 
+Quote Post
Chesso
post Jul 27 2006, 11:43 AM
Post #7


Teh Coder
Group Icon

Group: Members
Posts: 1,053
Joined: 18-April 06
From: Australia
Member No.: 12,833



I usually just get the necessary information first and check it when needed later on.....

Or with user things their username/password is registered using $_SESSION and sometimes other bits of data that I can verify their signed in and who they are and whether they are banned and such.
Go to the top of the page
 
+Quote Post
vhortex
post Jul 27 2006, 05:18 PM
Post #8


Guilty Until Proven Innocent
Group Icon

Group: Members
Posts: 372
Joined: 13-April 05
Member No.: 3,937



QUOTE(yordan @ Jul 26 2006, 06:41 AM) *

It seems that nightfox starts learning what is a relationnal database, and why using relations is more interesting than using single tables.


on my first tries.. i completely rewritten and restructured my whole database..
i started on 1 table then jump to 5 tables..

since there is no connection, the database is easy..
when i consider space saving and verification system..

i need to link them together.. how painfull the headache was..

--

when i bump into transactional database.. the pain and suffering grows much more..

i do now most of the initial designs on paper and always takes 3 rechecks before
i create the actual database..

--
thanks to nested quries, i have manage to avoid joins.. =)
Go to the top of the page
 
+Quote Post
nightfox
post Aug 3 2006, 11:37 PM
Post #9


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



OK... I'm stuck and getting frustrated... I have two tables:
-users
-content

The users table contains the following:
-id
-user
-pass
-access_name

The content table contains the following:
-id
-content

Here's my test file code:
CODE

<?php
$username = "root";
$password = "";
$hostname = "localhost";    
$database = "join-test";
$dbh = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL");
mysql_select_db($database)
    or die("Unable to connect to database. Check connection settings.");
// #############################################
$active_user = $_GET['id'];
// #############################################
$sql = "SELECT * FROM `users, content` WHERE `access_name`='".$active_user."' AND `users.id`=`content.id`";
$row = mysql_fetch_array(mysql_query($sql));
if($row['id']!="") {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
echo("User's profile:<br>".$row["content"]."");?>
</body>
</html>
<?php
}else{
die("<h1>Invalid ID or General Error</h1>");
}
?>
<?php
mysql_close($dbh);
?>

which gives me the following error whenever I try to access it even WITH a VALID access name:
QUOTE

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\mysql-joins\test.php on line 14
Invalid ID or General Error


I think I joined the tables wrong with:
$sql = "SELECT * FROM `users, content` WHERE `access_name`='".$active_user."' AND `users.id`=`content.id`";
but I'm not sure how it should be. I've been googling for about an hour and I'm really confused on how this should be!

[N]F
Go to the top of the page
 
+Quote Post
Chesso
post Aug 4 2006, 02:00 AM
Post #10


Teh Coder
Group Icon

Group: Members
Posts: 1,053
Joined: 18-April 06
From: Australia
Member No.: 12,833



Ok well from what I know, when asking for something from two differen things, I *don't think* (not sure) that you should have `` around the two, maybe around both or not at all.

Also using WHERE `something` = `something`, I don't think that it is right, try surrounding = `something` in single ''s instead of ``. Same for normal variables like $my_var you can just do, `someval` = '$someval'.

Hope that helps, I'm really puzzled as to why you surrounded two things in one set of `'s but it could be something I haven't needed to use yet.

Here's what I would try your sql as:

$sql = "SELECT * FROM `users`, `content` WHERE `access_name`='$active_user' AND `users.id`= 'content.id'";

How does users.id and content.id work anyway. You are most likely getting the error on trying to fetch the array because the syntax is wrong or something you try to grab does not exist (meaning it could still mean wrong syntax).

This post has been edited by Chesso: Aug 4 2006, 02:02 AM
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL - Trouble With Bulk Insert Statements(3)
  2. MySQL Realtime Replication(4)
  3. Recover Tables From A MySQL .frm File(8)
  4. Mirror My MySQL Database To Another Mysql Server(4)
  5. How To Connect MySQL With Flash?(8)
  6. MySQL Output Database Question(18)
  7. Linking Two Tables(12)
  8. Navcat For MySQL(8)
  9. Permission Problem With Mysql Database Creation(8)
  10. Mysql And Php(15)
  11. Problems With Php Saving Data Into Mysql(6)
  12. Login System Using A Mysql Db(5)
  13. Oracle Vs. Mysql Vs. Postgresql(9)
  14. Subqueries In Mysql(1)
  15. Apache Php With Mysql On Windows [solved](9)
  1. Not Understanding Mysql(4)
  2. Mysql Script Help(3)
  3. Mysql - So Hard(14)
  4. Mysql Problem(1)
  5. Sun Bought Mysql(6)
  6. Mysql Backup With Another Address?(4)
  7. I Have An Error With My Mysql Connection(7)
  8. Mysql And User File_priv(0)
  9. Mysql Database Management(1)
  10. Mysql Database Entry By Excel Sheets(2)
  11. Mysql On Computer(9)
  12. Any Website Provide Free Host Mysql Host?(4)
  13. Mysql Multiple Tables(1)


 



- Lo-Fi Version Time is now: 30th August 2008 - 12:52 PM