I have 3 tables :
tb_wrapper tb_doc tb_aut
===================== ==================== =======================
|id|web|title|source| |id| web | content | |id|web|aut | linkAut|
===================== ==================== =======================
|1 | A | AA | AAA | |1 | A | AAAA | |1 | A | B | C |
===================== ==================== |2 | A | D | E |
=======================
I wanna get all of record data from 3 table that has same web
.
here's my code :
$query = mysql_query("
SELECT
tb_wrapper.web,
tb_wrapper.title,
tb_wrapper.source,
tb_doc.web,
tb_doc.content,
tb_aut.web,
tb_aut.aut,
tb_aut.linkAut
FROM
tb_doc
INNER JOIN tb_wrapper ON tb_doc.web = tb_wrapper.web
AND
INNER JOIN tb_aut ON tb_doc.web = tb_aut.web
");
The error problem : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\sukses\indexsummary.php on line 87
.
EDIT
Then I wanna print them all :
while ($row = mysql_fetch_array($query)) {
$source = $row['source'];
$title = $row['title'];
$content = $row['content'];
$aut = $row['aut'];
$linkAut = $row['linkAut'];
echo '<h2><a href='.$source.'> '.$title.' </a></h2>';
echo '<p><a href='.$aut_url.'> '.$aut.' </a></p>';
}
And the result, they are printed twice. AA B and AA D. How to make them AA B D ?
Please help me. thanks in advance :)
$query = mysql_query("
SELECT
tb_wrapper.web,
tb_wrapper.title,
tb_wrapper.source,
tb_doc.web,
tb_doc.content,
tb_aut.web,
tb_aut.aut,
tb_aut.linkAut
FROM
tb_doc
INNER JOIN tb_wrapper ON tb_doc.web = tb_wrapper.web
INNER JOIN tb_aut ON tb_doc.web = tb_aut.web
YOu need to remove the AND between inner joins, that's invalid syntax.
You probably need to go do some more reading about how JOINS work in SQL. You seem to want to gather results from multiple rows, and that's generally not how it works.
If you're joining on a non-unique column from each table, you should expect to get repeated results for rows that have repeated values.
I may have not explained that properly, so more detail below. Here are your tables again as reference:
tb_wrapper tb_doc tb_aut
===================== ==================== =======================
|id|web|title|source| |id| web | content | |id|web|aut | linkAut|
===================== ==================== =======================
|1 | A | AA | AAA | |1 | A | AAAA | |1 | A | B | C |
===================== ==================== |2 | A | D | E |
=======================
Your query (without the AND as others have suggested) joins the 'web' column of each of the tables.
If you just look at your first 2 tables, they have only 1 row and 'web' is A. If you only joined these tables, you would get a 1-row result:
==================================
|web|title|source| web | content |
==================================
| A | AA | AAA | A | AAAA |
==================================
But your third table has 2 rows with the same 'web' value. The 1-row result from joining the first 2 tables can be joined to both rows of your third table. This produces the following result:
=====================================================
|web|title|source| web | content |web|aut | linkAut|
=====================================================
| A | AA | AAA | A | AAAA | A | B | C |
| A | AA | AAA | A | AAAA | A | D | E |
=====================================================
Your PHP then loops through the results and prints 2 columns ('title' and 'aut') from each row, resulting in:
AA B
AA D
This is exactly how it's supposed to work. If you want to get 'AA B D' as a result, simple iteration through rows will not be sufficient.
To see how it changes when values are unique, join all your rows on the 'id' column instead. Since each row's 'id' is unique in your sample, you will only get 1 row in your result.
If you're instead trying to get values returned from different rows (like showing the Title and Content once, but then a list of 'aut' values), you need to use 2 different queries or put some additional logic in your while
loop.