NEONecd999 Posted August 7, 2007 Share Posted August 7, 2007 How can I retrieve the name of the table from which a given MySQL result came from? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/63639-retrieve-table-name-of-mysql-result/ Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 show tables is the query that will return all the table in the db Quote Link to comment https://forums.phpfreaks.com/topic/63639-retrieve-table-name-of-mysql-result/#findComment-317100 Share on other sites More sharing options...
NEONecd999 Posted August 7, 2007 Author Share Posted August 7, 2007 show tables is the query that will return all the table in the db Sure, but that's not what I'm looking for... I have a query that is like SELECT * FROM table1, table2 If I pull a result from this query, mysql_result($result,$i,"fieldname") i'd like to know from which table, table1 or table2, did that result come from. Quote Link to comment https://forums.phpfreaks.com/topic/63639-retrieve-table-name-of-mysql-result/#findComment-317102 Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 show tables is the query that will return all the table in the db Sure, but that's not what I'm looking for... I have a query that is like SELECT * FROM table1, table2 If I pull a result from this query, mysql_result($result,$i,"fieldname") i'd like to know from which table, table1 or table2, did that result come from. actuallly this is wrong SELECT * FROM table1, table2 it should be the work of joining clause now on your question i say both but is not retrieving the data properly thats you also dont know where but doing that knid of query wont give a syntax errror but output error lol Quote Link to comment https://forums.phpfreaks.com/topic/63639-retrieve-table-name-of-mysql-result/#findComment-317103 Share on other sites More sharing options...
NEONecd999 Posted August 7, 2007 Author Share Posted August 7, 2007 okay, i'll be more explicit.. $result = mysql_query("SELECT * FROM tableA UNION SELECT * FROM tableB ORDER BY RAND()"); echo mysql_result($result,0,"field"); //from which table was this result since both tables were used in this query, and the results were randomly ordered, the first result from the query could be from either table. I need something that will return the name of the original table of this result. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/63639-retrieve-table-name-of-mysql-result/#findComment-317131 Share on other sites More sharing options...
hitman6003 Posted August 7, 2007 Share Posted August 7, 2007 Use php to randomly select the table, then use mysql to randomly select a row...I'm assuming that's what you want... $tables = array('table1', 'table2'); $which = array_rand($tables); $result = mysql_query("SELECT fieldname FROM " . $tables[$which] . " ORDER BY RAND() LIMIIT 1") or die(mysql_error()); echo 'Result from table ' . $tables[$which] . ' was: ' . mysql_result($result, 0); Quote Link to comment https://forums.phpfreaks.com/topic/63639-retrieve-table-name-of-mysql-result/#findComment-317136 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.