Topic: add new field to lengthy dbquery (Read 2595 times)
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
Posts: 886
1148 credits Members referred : 4
« on: Aug 23, 2006, 07:48:59 pm »
trying to add to this PHP mysql db query (thats why i posted here, its written in php form)
i want to make it so this very same "$result" will find the "user_clanrank" field from the db fusion_users aka $dbprefix_users... but i cant get it to work, if you understand my problem take a look:
Code:
<?php $result = dbquery( "SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu2.user_name AS user_lastuser FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC" ); ?>
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #1 on: Aug 23, 2006, 08:40:51 pm »
first, this is a MySQL question (move the post you're a mod too)
You have to debug the sql statement, just "echo" the statement and place the sql into the sql textarea inside phpmyadmin an see what happens (post the error if you get one...)
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
Posts: 886
1148 credits Members referred : 4
« Reply #2 on: Aug 23, 2006, 11:24:41 pm »
well nothin happens when i run the statement above, this is because it is stock and unchanged.. so it will not error, and it will grab all the data necessary, i just want to add to it so it finds the user_Clanrank from users table as well
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Aug 24, 2006, 12:11:53 am »
Nick said this before its difficult to help you without knowing the appliaction but try this:
"SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu1.user_Clanrank AS clan_rank, tu2.user_name AS user_lastuser FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC"
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
Posts: 886
1148 credits Members referred : 4
« Reply #4 on: Aug 24, 2006, 03:21:43 am »
nope didnt work.. here is entire page code if it helps, you can find this DBquery in the page twice.
Code:
<?php require_once "../maincore.php"; require_once BASEDIR."subheader.php"; include LOCALE.LOCALESET."forum/main.php"; include BASEDIR."colors2.php"; if (!isset($lastvisited) || !isNum($lastvisited)) $lastvisited = time(); $threads_per_page = 20;
if (!FUSION_QUERY || !$forum_id || !isNum($forum_id)) fallback("index.php");
$result = dbquery( "SELECT f.*, f2.forum_name AS forum_cat_name FROM ".$db_prefix."forums f LEFT JOIN ".$db_prefix."forums f2 ON f.forum_cat=f2.forum_id WHERE f.forum_id='".$forum_id."'" ); if (dbrows($result)) { $data = dbarray($result); if (!checkgroup($data['forum_access']) || !$data['forum_cat']) fallback("index.php"); } else { fallback("index.php"); } $can_post = checkgroup($data['forum_posting']);
$rows = dbrows(dbquery("SELECT * FROM ".$db_prefix."threads WHERE forum_id='$forum_id' AND thread_sticky='0'")); if (!isset($rowstart) || !isNum($rowstart)) $rowstart = 0;
if ($rowstart == 0) { $result = dbquery( "SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu1.user_Clanrank AS user_clanrank, tu2.user_name AS user_lastuser FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC"
if ($rows != 0) { $result = dbquery( "SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu1.user_Clanrank AS user_clanrank, tu2.user_name AS user_lastuser FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC"
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
Posts: 886
1148 credits Members referred : 4
« Reply #12 on: Aug 26, 2006, 06:20:58 pm »
nikolas, how would I put that into this existing statement:
Code:
<?php $result = dbquery( "SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu2.user_name AS user_lastuser FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC" ); ?>
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5778
46265 credits Members referred : 3
« Reply #13 on: Aug 26, 2006, 06:28:48 pm »
SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu2.user_name AS user_lastuser , rank_name FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id INNER JOIN fusion_clanranks ON ( fusion_clanranks.rank_id = fusion_users.user_clanrank ) WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
Posts: 886
1148 credits Members referred : 4
« Reply #14 on: Aug 26, 2006, 06:35:21 pm »
i had to change it around abit, because i want the rank_color not the rank_name.. but this is the error i get:
Quote
Unknown table 'fusion_users' in on clause
here is current PHP code
Code:
<?php $result = dbquery(" SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu2.user_name AS user_lastuser , rank_color FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id INNER JOIN ".$db_prefix."clanranks ON ( ".$db_prefix."clanranks.rank_id = ".$db_prefix."users.user_clanrank ) WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC" ); ?>
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5778
46265 credits Members referred : 3
« Reply #15 on: Aug 26, 2006, 06:44:33 pm »
What about this?
Code:
<?php $result = dbquery(" SELECT t.*, COUNT(p.post_id) AS thread_replies, tu1.user_name AS user_author, tu2.user_name AS user_lastuser , rank_color FROM ".$db_prefix."threads t LEFT JOIN ".$db_prefix."posts p USING(thread_id) LEFT JOIN ".$db_prefix."users tu1 ON t.thread_author = tu1.user_id LEFT JOIN ".$db_prefix."users tu2 ON t.thread_lastuser = tu2.user_id INNER JOIN ".$db_prefix."clanranks ON ( ".$db_prefix."clanranks.rank_id = tu1.user_clanrank ) WHERE t.forum_id='$forum_id' AND thread_sticky='1' GROUP BY thread_id ORDER BY thread_lastpost DESC" ); ?>
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
Posts: 886
1148 credits Members referred : 4
« Reply #18 on: Aug 26, 2006, 07:11:57 pm »
yes i know, i set the Username link style="color: $mm_color" and then it includes the file colors2.php which actually has the data and if/else statements to find their color based on clanrank