mahenda Posted July 3, 2020 Share Posted July 3, 2020 I have two table in my database combined by using UNION, when it come to searching via search form, all data are retrieved as expected, but the problem come when i click the link that has the data from second table it redirecting to the page with first table details. Assume i have retrieved data using while loop and the UNION. while($search = $query->fetch()) {?> <div> <a href="pageone.php?po=<?php echo $search['pr_id'];?>">Read More...</a> </div> <?php }?> //the result become <a href="pageone.php?po=1">Read More...</a> <a href="pageone.php?pt=3">Read More...</a> //But what if data come from both first and second table in a database, i want the link become to be as <a href="pageone.php?po=1">Read More...</a> <a href="pagetwo.php?pt=3">Read More...</a> // any idea please Quote Link to comment Share on other sites More sharing options...
requinix Posted July 3, 2020 Share Posted July 3, 2020 You have to put something in your query so that you can tell where each result is coming from. Then use that to output the correct link. Quote Link to comment Share on other sites More sharing options...
mahenda Posted July 4, 2020 Author Share Posted July 4, 2020 20 hours ago, requinix said: You have to put something in your query so that you can tell where each result is coming from. Then use that to output the correct link. My query look like this SELECT pr_id, pr_name FROM pr WHERE pr_name LIKE :q UNION SELECT pt_id, pt_name FROM pt WHERE pt_name LIKE :q The problem is when we search it combine all pr_id and pt_id in a single column and all I'd can be retrieved by using pr_id. So when user click the link which carry pt_id it redirect to the page with pr_id details. Help Quote Link to comment Share on other sites More sharing options...
requinix Posted July 4, 2020 Share Posted July 4, 2020 I understand what the problem is. Do you understand what I said the solution is? Quote Link to comment Share on other sites More sharing options...
mahenda Posted July 5, 2020 Author Share Posted July 5, 2020 18 hours ago, requinix said: I understand what the problem is. Do you understand what I said the solution is? No Quote Link to comment Share on other sites More sharing options...
requinix Posted July 5, 2020 Share Posted July 5, 2020 On 7/4/2020 at 8:31 AM, mahenda said: My query look like this SELECT pr_id, pr_name FROM pr WHERE pr_name LIKE :q UNION SELECT pt_id, pt_name FROM pt WHERE pt_name LIKE :q Look at what that query does. It returns an ID and a name. But how do you know what table they came from? You don't. You need to put something into the query that tells you the table. Something that is different for the two table sources. For example, one could use the string "pr" and the other "pt". Or maybe a nicer value than those, such as something I wouldn't know because "pr" and "pt" are terrible names for guessing about what their tables are for. Or why there are two tables as similar as they are. Quote Link to comment Share on other sites More sharing options...
mahenda Posted July 9, 2020 Author Share Posted July 9, 2020 thank you guys, it is working, i added alias on each table column and using if condition to detect if the data come from first table redirect to first page else second page Quote Link to comment Share on other sites More sharing options...
mahenda Posted July 9, 2020 Author Share Posted July 9, 2020 But i have this question why I get undefined variable, when the field in mysql database is set to null value/type. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 9, 2020 Share Posted July 9, 2020 You get an undefined variable warning because the variable was not defined. Since you haven't posted the code for it, my guess is that you might have misspelled its name somewhere. 1 Quote Link to comment 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.