aram Posted April 29, 2011 Share Posted April 29, 2011 Hi im selectin records from three tables by union all, the records is news title which when i display to users it will be like a link when users click the link redirects him/her to th news page by (id) of the news but the problem is i can receive from one table but when its two table i dont know how to get table names my code is like that: <?php $result = mysql_query('(SELECT * FROM tbl1) UNION ALL(SELECT * FROM tbl2) UNION ALL(SELECT * FROM tbl3) ORDER BY date DESC LIMIT 4')or exit(mysql_error()); while($row = mysql_fetch_array($result)){ echo"<h2><a href='news.php?id=" . $row['id'] . "'>" . $row['title'] . "</a></h2>"; } ?> Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 Any help please Link to comment Share on other sites More sharing options...
birbal Posted April 29, 2011 Share Posted April 29, 2011 i am not sure..what do you mean by getting table names? Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 i dont know really if this problem will be solved by getting table names or another wayif you looked a t the code it will get 4 links from database wihch its from three tables, when you click th links from the first page redirects the user to the news pagethe news page code is like this: <?phpmysql_connect("localhost","root","");mysql_select_db("database_name");$tbl_name="tbl1";$id=$_GET[id];$result = mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");while($rows=mysql_fetch_array($result)){echo $rows[news];}?> but the problem is its three tables from the first page thanks for any reply Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 i am not sure..what do you mean by getting table names?Do you know what i mean now? Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 comon people i know my english is bad but my codes tells whats the problem.i need the answer please Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 I will reask my question:i have three links in th (index.php) the first link from table 1 and the second one from table2 and the third one from table three, All of them redirects to the one page (News.php).the links is like that: <a href='news.php?id=" . $row[id] . "'> but there is something mising which its how to know this link is from the second table or third table or first table i mean there most be something like this: <a href='news.php?id=" . $row[id] . "&" . table=tablename . "'> Link to comment Share on other sites More sharing options...
jeffman Posted April 29, 2011 Share Posted April 29, 2011 aram, language is not the issue here. You just have to be patient. You have only been waiting a few hours, and many of the senior board members are asleep or only now beginning their days. Before tomorrow, someone with enough SQL expertise to help you will probably find your question. (Actually, it might take longer, since weekends are slower than workdays.) Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 aram, language is not the issue here. You just have to be patient. You have only been waiting a few hours, and many of the senior board members are asleep or only now beginning their days. Before tomorrow, someone with enough SQL expertise to help you will probably find your question. (Actually, it might take longer, since weekends are slower than workdays.) Thank you Deirdre's you are right i have to wait i will take your advise thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2011 Share Posted April 29, 2011 $result = mysql_query('(SELECT id, title, 'tbl1' AS `table_name` FROM tbl1) UNION ALL(SELECT id, title, 'tbl2' AS `table_name` FROM tbl2) UNION ALL(SELECT id, title, 'tbl3' AS `table_name` FROM tbl3) ORDER BY date DESC LIMIT 4')or exit(mysql_error()); Now you have a field called table_name with the name of the table that the rows came from. You can add the table name to the link if you want to. Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 Thats what i want actually to add the table-names to the link can you advise me how? to try both together.thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2011 Share Posted April 29, 2011 echo"<h2><a href='news.php?id=" . $row['id'] . "&table=" . $row['table_name'] . "'>" . $row['title'] . "</a></h2>"; Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 echo"<h2><a href='news.php?id=" . $row['id'] . "&table=" . $row['table_name'] . "'>" . $row['title'] . "</a></h2>";Thanks my friend can you explain how is this work or send me a link, because im getting nothing the link is like this when you click ithttp://localhost/mywebsite/news.php?id=1&table=i mean explaining this$result = mysql_query('(SELECT id, title, 'tbl1' AS `table_name` FROM tbl1) UNION ALL(SELECT id, title, 'tbl2' AS `table_name` FROM tbl2) UNION ALL(SELECT id, title, 'tbl3' AS `table_name` FROM tbl3) ORDER BY date DESC LIMIT 4')or exit(mysql_error()); Thanks Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 Thank you very much justsomeguy i have solved the problem and i understand the code.Thank you very much again. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2011 Share Posted April 29, 2011 That query adds a field called table_name to the result set with the name of each table (or whatever you want). You can use something like this to print the values in the row: while($rows=mysql_fetch_array($result)){ print_r($rows);} You should see the table_name field in each row with the values from the query. Link to comment Share on other sites More sharing options...
aram Posted April 29, 2011 Author Share Posted April 29, 2011 That query adds a field called table_name to the result set with the name of each table (or whatever you want). You can use something like this to print the values in the row:while($rows=mysql_fetch_array($result)){ print_r($rows);} You should see the table_name field in each row with the values from the query. I Really apreciate it thank you i have learned so much until now. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.