Jump to content

How to use joined querys.


Andy11548

Recommended Posts

Just a quick question, I got told I should use joined querys, how would I use this query?

 

SELECT * FROM `forum_cats` AS cats, `forum_subs` AS subs, `forum_topics` AS topics, `forum_replys` AS replys, `users` AS users WHERE subs.cat=cats.name AND topics.sub_cat_id=subs.id AND replys.sub_cat_id=subs.id AND users.username=replys.username ORDER BY cats.id ASC, replys.id DESC

Link to comment
https://forums.phpfreaks.com/topic/241703-how-to-use-joined-querys/
Share on other sites

Well, I get this error...

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in C:\wamp\www\test.php on line 6

 

OR

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\test.php on line 6

Sure thing

 

<?php
session_start();
include './includes/mysql/connect.php';

$query = 
("
SELECT *
FROM `forum_cats` AS cats, `forum_subs` AS subs, `forum_topics` AS topics, `forum_replys` AS replys, `users` AS users
WHERE subs.cat = cats.name
AND topics.sub_cat_id = subs.id
AND replys.sub_cat_id = subs.id
AND users.username = replys.username
ORDER BY cats.id ASC , replys.id DESC
") or die(mysql_error());
$fetch = mysql_fetch_array($query);
?>

 

It's on my test page so I never ruined the rest of my code, so thats litterally everything.

 

I don't like this joined query stuff, haha :).

you havent executed the query. You need to execute the query to get the resource id THEN use any fetch commands to retrievfe the data. it should look something like this:

<?php
session_start();
include './includes/mysql/connect.php';

$query = 
("
SELECT *
FROM `forum_cats` AS cats, `forum_subs` AS subs, `forum_topics` AS topics, `forum_replys` AS replys, `users` AS users
WHERE subs.cat = cats.name
AND topics.sub_cat_id = subs.id
AND replys.sub_cat_id = subs.id
AND users.username = replys.username
ORDER BY cats.id ASC , replys.id DESC
") ;
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){

// output the results . ie $row['item1'];

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.