jeff5656 Posted May 2, 2011 Share Posted May 2, 2011 I want to join 3 tables. The records are linked by id. news_read has a field called users_id which links to the users tables. I get "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource" $query = "SELECT *, news_read.id as newsid FROM news_read, users INNER JOIN news ON news_read.news_id = news.id INNER JOIN news_read.user_id=users.id order by news_date DESC"; ; here's my 3 tables and the way they are linked by id USERS NEWS_READ NEWS id --------------------> user_id news_id ------------------------>id Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/ Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 can i see the full code snippet please Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209442 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 can i see the full code snippet please it's just this: (is that what you're asking? I just put it into a while loop. $results = mysql_query($query); while ($row = mysql_fetch_assoc ($results)) { Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209444 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 Hmm, I thought I found the error but this doesn't work either: $query = "SELECT *, news_read.id as newsid FROM news_read, users INNER JOIN news ON news_read.news_id = news.id INNER JOIN users ON news_read.user_id=users.id order by news_date DESC" Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209448 Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 try puting this to display the error $results = mysql_query($query) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209450 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 try puting this to display the error $results = mysql_query($query) or die(mysql_error()); The plot thickens. Here's the error: Not unique table/alias: 'users' But the name of my table is users, and there is no other table called users... Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209454 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 ugh.. fugix was really helping me out when the thread was moved. I doubt he knows to look here. Couldn't you guys have moved the thread AFTER he answered my question? Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209462 Share on other sites More sharing options...
Maq Posted May 2, 2011 Share Posted May 2, 2011 ugh.. fugix was really helping me out when the thread was moved. I doubt he knows to look here. Couldn't you guys have moved the thread AFTER he answered my question? There is a redirect from the original section & also if he clicks "Show new replies to your posts." this thread will still show up. Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209467 Share on other sites More sharing options...
Maq Posted May 2, 2011 Share Posted May 2, 2011 You need to alias your table name: $query = "SELECT *, news_read.id as newsid FROM news_read, users u INNER JOIN news ON news_read.news_id = news.id INNER JOIN u ON news_read.user_id=u.id order by news_date DESC" Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209471 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 You need to alias your table name: When I do that I get this error: Not unique table/alias: 'u' Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209479 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 Here are my tables. Can anyone fgure out why I get that error message? CREATE TABLE IF NOT EXISTS `news_read` ( `id` int(10) NOT NULL auto_increment, `user_id` int(10) NOT NULL, `news_id` int(10) NOT NULL, `signoff_status` enum('a','s') collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) --- CREATE TABLE IF NOT EXISTS `news` ( `id` int(10) NOT NULL auto_increment, `news_date` date NOT NULL, `news` text collate utf8_unicode_ci NOT NULL, `group_type` varchar(20) collate utf8_unicode_ci NOT NULL, `institution` varchar(30) collate utf8_unicode_ci NOT NULL, `exp_date` date NOT NULL, `signoff_status` enum('a','s') collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) CREATE TABLE IF NOT EXISTS `users` ( `id` int(20) NOT NULL auto_increment, [snip] `field_name99` varchar(60) NOT NULL, PRIMARY KEY (`id`) ) Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209498 Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 try this $query = "SELECT *, news_read.id as newsid FROM news_read, users INNER JOIN news ON news_read.news_id = news.id INNER JOIN news_read.user_id=u2.id order by news_date DESC"; Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209536 Share on other sites More sharing options...
Zane Posted May 2, 2011 Share Posted May 2, 2011 Your second join doesn't JOIN any tables, therefore it's useless. Also, you're selecting from two tables when your selections are distinctly for one...other than the asterisk. Your query should look something like this SELECT * FROM news_read nr INNER JOIN news n ON n.id = nr.news_id INNER JOIN users u ON u.id = nr.user_id ORDER by n.news_date DESC Then, assuming that works, you can trade that asterisk for the fields you DO want... like the actual news text and what not. SELECT nr.news_text, nr.news_date FROM news_read nr INNER JOIN news n ON n.id = nr.news_id INNER JOIN users u ON u.id = nr.user_id ORDER by n.news_date DESC Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209551 Share on other sites More sharing options...
jeff5656 Posted May 2, 2011 Author Share Posted May 2, 2011 Zanus: That worked perfectly. Thanks!! Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209689 Share on other sites More sharing options...
jeff5656 Posted May 3, 2011 Author Share Posted May 3, 2011 And thank you to fugix too. I appreciate your help. :-) Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209871 Share on other sites More sharing options...
fugix Posted May 3, 2011 Share Posted May 3, 2011 no problem. glad we could help Link to comment https://forums.phpfreaks.com/topic/235343-joining-3-tables-and-get-not-a-valid-mysql-result-resource/#findComment-1209874 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.