cmb Posted August 10, 2011 Share Posted August 10, 2011 i have this union statement that returns exactly what i want SELECT c_day_entered FROM Comments WHERE pid = '22' UNION SELECT wv_day_entered FROM walk_in_visit WHERE wv_pid = '22' UNION SELECT day_entered FROM first_visit WHERE fv_pid = '22' ORDER BY 1 DESC Now i want to using php display this info but idk how to do this help plz Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/ Share on other sites More sharing options...
kickstart Posted August 10, 2011 Share Posted August 10, 2011 Hi Alias the column names using a common name in each select, or (nastier) use mysql_fetch_row and refer to the column number. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255184 Share on other sites More sharing options...
cmb Posted August 10, 2011 Author Share Posted August 10, 2011 I tried this but it doesnt give me all the data it should include_once "connecttodatabase/database.php"; $id = $_GET['id']; $query = "SELECT c_day_entered FROM Comments WHERE pid = $id UNION SELECT wv_day_entered FROM walk_in_visit WHERE wv_pid = $id UNION SELECT day_entered FROM first_visit WHERE fv_pid = $id ORDER BY 1 DESC" or die ('Error With Database ' .mysql_error()); $result = mysql_query($query) or die("Query failed ($query) - " . mysql_error()); while(mysql_fetch_row($result)){ print_r (mysql_fetch_row($result)); echo "<br />"; } // disconnect mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255454 Share on other sites More sharing options...
fenway Posted August 10, 2011 Share Posted August 10, 2011 If you call mysql_fetch_row() twice, you'll be skipping every other result. Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255469 Share on other sites More sharing options...
cmb Posted August 10, 2011 Author Share Posted August 10, 2011 when i get rid of the while and just have it as print_r(mysql_fetch_row($result)); i only get 1 result when i should be getting 8 and im sure i should be getting 8 because ive tested the query with mysql workbench Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255484 Share on other sites More sharing options...
DavidAM Posted August 10, 2011 Share Posted August 10, 2011 Fetch the data into a variable in the while statement, and work with the variable: while($row = mysql_fetch_row($result)){ print_r ($row); echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255494 Share on other sites More sharing options...
cmb Posted August 10, 2011 Author Share Posted August 10, 2011 Thank you thats what i wanted but one more thing is their anyway to get rid of Array ( [0] => when it shows the results Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255503 Share on other sites More sharing options...
DavidAM Posted August 10, 2011 Share Posted August 10, 2011 print_r just dumps the array; it is useful for debugging. For output, just print (or echo) the variable. In this case, $row is an array, so you have to print/echo the array elements. print($row[0]); Quote Link to comment https://forums.phpfreaks.com/topic/244371-union-statements/#findComment-1255508 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.